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 on April 23, 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   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 18:20:11 +0100

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/


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