Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Steve Nakoneshny <scnakone@ucalgary.ca> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |
Subject | Re: st: foreach / forvalues loop error |
Date | Mon, 29 Aug 2011 08:57:10 -0600 |
Thank you Tirthankar, Eric and Nick. With your help, I was able to make it work successfully! Thanks, Steve ~~~~~~~~~~~~~~~~~~~~ Steve Nakoneshny Research Assistant Ohlson Research Initiative University of Calgary - Faculty of Medicine HRIC 2A02 3280 Hospital Dr. NW Calgary AB T2N 4Z6 Tel: (403) 220-4347 Fax: (403) 270-3145 www.ohlsonresearch.ca On 2011-08-26, at 11:37 AM, Tirthankar Chakravarty wrote: > Steve, > > I think that the way you have it set up now is actually safer coding, > but if you are sure about the order of items in your local then here > is the concision you are looking for. The relevant loop is the last > one. > > /*******************************************/ > clear* > > // generate some data > set obs 110 > gen primsitenum = . > g primary_site="" > > // email readable local declaration (local now a string) > local x "Oral Cavity Oropharynx Hypopharynx Larynx" > local x "`x' Nasopharynx Paranasal Sinus Skin Salivary" > local x "`x' Gland Unknown Primary Thyroid Other Site" > > forv i=0/10 { > replace primary_site = "`:word `=`i'+1' of `x''" /// > in `=`i'*10+1'/`=(`i'+1)*10' > } > > // make replacements > forvalues i = 1/11 { > replace primsitenum = `i' if /// > primary_site =="`: word `i' of `x''" > } > /****************************************************/ > > T > On Fri, Aug 26, 2011 at 10:26 AM, Eric Booth <ebooth@ppri.tamu.edu> wrote: >> <> >> >> 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/ >> > > > > -- > Tirthankar Chakravarty > tchakravarty@ucsd.edu > tirthankar.chakravarty@gmail.com > > * > * 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/