Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: editing string variables to remove letters and keep only numbers


From   Michael McCulloch <mm@pinestreetfoundation.org>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: editing string variables to remove letters and keep only numbers
Date   Tue, 18 Jun 2013 10:34:05 -0700

Thanks Nick and Rich,
These comments helped me get started on understanding forval.

And in Nick's interpretive instructions, I discovered my error:

If I run this entire section of code in one do-file command, it is successful:
gen length = length(id)
su length, meanonly
local lmax = r(max)
gen numstr = ""
gen letterstr = ""
forval i = 1/`lmax'{
     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)
}

However, when i was running the first 5 lines and then the forval separately, the syntax error message occurred.
Why is that?

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 18, 2013, at 10:20 AM, Nick Cox wrote:

> Rich is right. The example code was meant to show how simple the idea
> was; it seems that was a failure.
> 
> Starting again, and correcting a typo of mine.
> 
> #1 What's the length of the longest string?
> 
> #1.1 What is the length of each string value?
> gen length = length(strvar)
> 
> #1.2 Summarize that
> su length, meanonly
> 
> #1.3 Put the maximum in a local macro
> local lmax = r(max)
> 
> #2 Initialise variables to hold numeric and non-numeric characters:
> gen numstr = ""
> gen letterstr = ""
> 
> 
> #3 loop over characters from first to last
> forval i = 1/`lmax' {
> 
> #3.1 one command line only
> # if i th character is between 0 and 9, it's a numeric character, so
> add it to numeric character variable
> 
> replace numstr = numstr + substr(strvar, `i', 1) if
> inrange(real(substr(strvar, `i', 1)), 0, 9)
> 
> #3.2 one command line only
> #otherwise add it to non-numeric character variable
> 
> replace letterstr = letterstr + substr(strvar, `i', 1) if
> !inrange(real(substr(strvar, `i', 1)), 0, 9)
> 
> }
> 
> Nick
> njcoxstata@gmail.com
> 
> 
> On 18 June 2013 17:03, Richard Goldstein <richgold@ix.netcom.com> wrote:
> 
>> looks to me like you are putting what should be on one line onto two
>> lines without the "///" continuation; note that in the code below, the
>> loop should only have two lines, each starting with "replace"
> 
> On 6/18/13 12:00 PM, Michael McCulloch wrote:
> 
>>> Nick, as it's NOW written (with your correction included), I still receive an "invalid syntax" error.
>>> This is my first exercise in learning -forval-, so even after reviewing the -help- I'm not sure how to troubleshoot further.
>>> 
>>> forval i = 1/`lmax' {
>>>     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)
>>> }
>>> 
> *
> *   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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index