Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <njcoxstata@gmail.com> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |
Subject | Re: st: editing string variables to remove letters and keep only numbers |
Date | Tue, 18 Jun 2013 01:09:18 +0100 |
Should be forval i = 1/`lmax' { Nick njcoxstata@gmail.com On 18 June 2013 01:06, Michael McCulloch <mm@pinestreetfoundation.org> wrote: > In implementing the example, I wrote, based on the variable "id": > > gen length = length(id) > su length, meanonly > local lmax = r(max) > gen numstr = "" > gen letterstr = "" > > forval i = 1/`max' { > replace numstr = numstr + substr(id, `i', 1) if > inrange(real(substr(id, `i', 1)), 0, 9) > replace letterstr = letterstr + substr(id, `i', 1) if > !inrange(real(substr(id, `i', 1)), 0, 9) > } > > The forval statement is where I am getting the invalid syntax error. > > > Best wishes, > Michael McCulloch, LAc MPH PhD > > -- > Pine Street Foundation, since 1989 > 124 Pine Street | San Anselmo | California | 94960-2674 > P: (415) 407-1357 | F: (206) 338-2391 | http://www.PineStreetFoundation.org > > On Jun 17, 2013, at 5:02 PM, Nick Cox wrote: > >> Yes. The negation is intended. If a character isn't 0 to 9, it's >> regarded as a a letter. >> >> Show us the exact code you typed to get a better answer. >> Nick >> njcoxstata@gmail.com >> >> >> On 18 June 2013 00:51, Michael McCulloch <mm@pinestreetfoundation.org> wrote: >>> Thanks Nick. >>> >>> On the second inrange argument, does the "!" belong there? >>> When I run that forval command, an invalid syntax r(198) code is returned. >>> >>> >>> >>> Best wishes, >>> Michael McCulloch, LAc MPH PhD >>> >>> -- >>> Pine Street Foundation, since 1989 >>> 124 Pine Street | San Anselmo | California | 94960-2674 >>> P: (415) 407-1357 | F: (206) 338-2391 | http://www.PineStreetFoundation.org >>> >>> On Jun 17, 2013, at 4:10 PM, Nick Cox wrote: >>> >>>> There are is a dedicated functions in -egenmore- (SSC) (-sieve()-) >>>> but let's take it from first principles. >>>> >>>> gen length = length(strvar) >>>> su length, meanonly >>>> local lmax = r(max) >>>> >>>> gen numstr = "" >>>> gen letterstr = "" >>>> >>>> forval i = 1/`max' { >>>> replace numstr = numstr + substr(strvar, `i', 1) if >>>> inrange(real(substr(strvar, `i', 1)), 0, 9) >>>> replace letterstr = letterstr + substr(strvar, `i', 1) if >>>> !inrange(real(substr(strvar, `i', 1)), 0, 9) >>>> } >>>> Nick >>>> njcoxstata@gmail.com >>>> >>>> >>>> On 17 June 2013 23:53, Michael McCulloch <mm@pinestreetfoundation.org> wrote: >>>>> I have a variable in my dataset that (due to changes in data entry practices over time) contains several styles of the variable ID: >>>>> >>>>> - a number (e.g. 164) >>>>> - a letter-number combination (e.g. e64) >>>>> - a comma-separated letter-number combination (e.g. e64,e65) >>>>> >>>>> In seeking to (A) remove the letters, and (B) separate the comma-separated into two separate variables, ID1 and ID2, I wrote the following argument: >>>>> >>>>> . split ID, p(",") >>>>> . gen str id1_new ="" // make new ID to separate out the "e" from ID >>>>> . replace id1_new=substr(id1,2,3) >>>>> >>>>> >>>>> This successfully splits ID into ID1 and ID2. >>>>> >>>>> This also works if: >>>>> a 3-digit variable has a preceding letter (e64 is changed to 64) >>>>> >>>>> However, in the case of a 3-digit values WITHOUT PRECEDING LETTER, the first digit is removed (164 is changed to 64). >>>>> >>>>> Any suggestions would be appreciated. >>>>> >>>>> >>>>> Best wishes, >>>>> Michael McCulloch, LAc MPH PhD >>>>> >>>>> -- >>>>> Pine Street Foundation, since 1989 >>>>> 124 Pine Street | San Anselmo | California | 94960-2674 >>>>> P: (415) 407-1357 | F: (206) 338-2391 | http://www.PineStreetFoundation.org >>>>> >>>>> >>>>> * >>>>> * For searches and help try: >>>>> * http://www.stata.com/help.cgi?search >>>>> * http://www.stata.com/support/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/