Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: RE: trimming leading numbers from a string.


From   "Matt Spittal" <[email protected]>
To   <[email protected]>
Subject   RE: st: RE: trimming leading numbers from a string.
Date   Tue, 23 Sep 2008 16:50:04 +1000

Ashim,

One approach would be to count the number of words using the function -wordcount- and use this as the basis for returning the string.  Assuming the string you want returned always starts in the third position (when there is more than one word), then you can do the following:

	generate wcount = wordcount(cname)
	generate place = cname if wcount == 1			// only one word in cname
	replace place = substr(cname, 3, .) if wcount >= 2	// more than one word

In this case, the function -substr- returns everything in cname from the third position onwards. If, say, you only wanted to return information from the 3rd to the 7th position, you could type -substr(cname, 3, 7)- instead.  

-- Matt
[email protected]



-----Original Message-----
From: [email protected]
[mailto:[email protected]]On Behalf Of Ashim Kapoor
Sent: Tuesday, 23 September 2008 3:58 PM
To: [email protected]
Subject: Re: st: RE: trimming leading numbers from a string.


ahhh I get you. I was thinking more in terms of "detecting" a number
and replacing it with a space and then trimming. But Thank You for
your reply.

Is there a way to say ALL except the 1st word ? That would be very
useful cos sometimes I  have " 0 Andhra Pradesh ".

Thank you,
Ashim.

On Tue, Sep 23, 2008 at 11:13 AM, Matt Spittal
<[email protected]> wrote:
> Ashim,
>
> You can use the string functions, see -help string functions- to extract the information that you want. For instance
>
>        generate place = word(cname, -1)
>
> will return the last word in the variable cname. Similarly,
>
>        generate place = word(cname, 2)
>
> will return the second word (although this may not be what you want as there is only one word in your first example).
>
> -- Matt
> [email protected]
>
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]]On Behalf Of Ashim Kapoor
> Sent: Tuesday, 23 September 2008 3:07 PM
> To: [email protected]
> Subject: st: trimming leading numbers from a string.
>
>
> Hello everyone,
>
>
> I have a string variable called "cname".
>
> It looks something like this.
>
>                 cname
> 1.        DELHI
> 2.    0 HYDERBAD
> 3.    1 USA
> 4.     4 AP
>
> I want to remove the leading 0 / 1 / 4 / other numbers from this
> string and keep only the alphabets. Is there a do file someone has
> written on this ?
>
> Thank you,
> Ashim.
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index