Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: foreach / forvalues loop error


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: foreach / forvalues loop error
Date   Fri, 26 Aug 2011 19:13:44 +0100

One small detail as an addendum to Eric's post:

local i `++i'

works to increment local macro i but

local ++i

is simpler.

Example:

. local i = 42

. local ++i

. di "`i'"
43

However, those used to ++ in C and other languages should note this:

. local i++

. di "`i'"
++

Before the ++ operator was introduced for Stata macros, there was
already a rule that whatever follows a legal macro name is its new
contents, which explains the example just given. However, ++ was
introduced to Mata at its (public) inception and is not subject to
this nuance.

A tutorial column on working through lists in parallel was given in

SJ-3-2  pr0009  . . . . . . . . . . . . . Speaking Stata:  Problems with lists
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q2/03   SJ 3(2):185--202                                 (no commands)
        discusses ways of working through lists held in macros

accessible to all at http://www.stata-journal.com/sjpdf.html?articlenum=pr0009

Nick

On Fri, Aug 26, 2011 at 6:26 PM, Eric Booth <[email protected]> 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
> ***

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