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: foreach / forvalues loop error


From   Eric Booth <ebooth@ppri.tamu.edu>
To   "<statalist@hsphsun2.harvard.edu>" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: foreach / forvalues loop error
Date   Fri, 26 Aug 2011 17:26:29 +0000

<>

On Aug 26, 2011, at 11:56 AM, Steve Nakoneshny wrote:

> <snip>

> As inelegant as this is, it works. The structure of this code leads me to think that it can be rewritten far simpler using a foreach loop. I am a relative neophyte with Stata and have just begun to explore the use of -foreach- and -forvalues-. I've written the following code as a first attempt:
> 
> ----
> gen primsitenum = .
> 
> local x Oral Cavity Oropharynx Hypopharynx Larynx Nasopharynx Paranasal Sinus Skin Salivary Gland Unknown Primary Thyroid Other Site
> 
> forvalues n = 1/11 {
> 	replace primsitenum = `n' if Primary_site =="`x'"
> }



Your loop is doing this in the first step:
> replace primsitenum = 1 if Primary_site=="Oral Cavity Oropharynx Hypopharynx Larynx Nasopharynx Paranasal Sinus Skin Salivary Gland Unknown Primary Thyroid Other "

though I think you expect it to do this:

> replace primsitenum = 1 if Primary_site=="Oral Cavity"

First of all, in your local x, you haven't told Stata how to differentiate "Oral Cavity" from "Oral", "Cavity", or "Cavity Oropharynx" -- how is it supposed to know which items in the list are single or multiple words?  The quick answer is to group them with quotes, like:

> local x  `"  `"Oral Cavity"' `"Oropharynx"'  `"Hypopharynx"' `"Larynx"' `"Nasopharynx"' `"Paranasal"' `"Sinus"' `"Skin Salivary"' `"Gland"' `"Unknown Primary"' `"Thyroid"' `"Other Site"' "'

Notice the use of double quotes -- see help quotes.
Next, you are currently iterating 'n' in the for values loop, but not 'x' -- you need to get it to loop over 'x' as well.  One approach is to -tokenize- (see help -token-) your 'x' macro and then loop over it with:

loc i = 1
tokenize `"`x'"'
while "`1'" != "" {
	replace primis  = `i' if Primary == "`1'"
	macro shift
	loc i `++i'
	}

which iterates across the `x' macro using the while loop and the 'macro shift' line and iterates your counter `i' with the `++i' macro expansion operator.  (See -help macro- and -help extended_fcn- for more)



Or try this MWE in a do-file:

****
sysuse auto, clear

g primis = .
loc x `"  `"AMC Concord"' `"Buick Electra"' "' 
loc i = 1
tokenize `"`x'"'
di `"`1'"'
while `"`1'"' != "" {
	di in g "`1'"
	di in r "`i'"
	replace primis  = `i' if make == `"`1'"'
	macro shift
	loc i `++i'
	}

order primis
***
- Eric

__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
ebooth@ppri.tamu.edu
Office: +979.845.6754



*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index