

> replace primis = `i' if Primary = "`1'" One approach is to -tokenize- (see help -token-) your 'x' macro and then loop over it with: > 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. > Notice the use of double quotes - see help quotes. > local x `" `"Oral Cavity"' `"Oropharynx"' `"Hypopharynx"' `"Larynx"' `"Nasopharynx"' `"Paranasal"' `"Sinus"' `"Skin Salivary"' `"Gland"' `"Unknown Primary"' `"Thyroid"' `"Other Site"' "'
#Stata local how to#
> 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: > replace primsitenum = 1 if Primary_site="Oral Cavity" > though I think you expect it to do this:
#Stata local skin#
> replace primsitenum = 1 if Primary_site="Oral Cavity Oropharynx Hypopharynx Larynx Nasopharynx Paranasal Sinus Skin Salivary Gland Unknown Primary Thyroid Other " > Your loop is doing this in the first step: In Stata this process is known as a macro. > replace primsitenum = `n' if Primary_site ="`x'" Stata allows you to use a single word, such as continuous, to represent many other words. > local x Oral Cavity Oropharynx Hypopharynx Larynx Nasopharynx Paranasal Sinus Skin Salivary Gland Unknown Primary Thyroid Other Site I've written the following code as a first attempt: I am a relative neophyte with Stata and have just begun to explore the use of -foreach- and -forvalues. The structure of this code leads me to think that it can be rewritten far simpler using a foreach loop.

> On Aug 26, 2011, at 11:56 AM, Steve Nakoneshny wrote: In my own work, I use macros for a wide range of tasks including storing le paths, constructing variable names in loops, accu-mulating graph commands and formatting. They come in two avors local and global and are an indispensable part of automating repetitive tasks.
#Stata local manual#
Coxĭiscusses ways of working through lists held in macros The Stata manual describes macros as ‘the variables of Stata programs’. Introduced to Mata at its (public) inception and is not subject toĪ tutorial column on working through lists in parallel was given in However, those used to ++ in C and other languages should note this:īefore the ++ operator was introduced for Stata macros, there wasĪlready a rule that whatever follows a legal macro name is its newĬontents, which explains the example just given.

One small detail as an addendum to Eric's post: Nick Cox st: foreach / forvalues loop error Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at.
