Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Using foreach to loop over macros

Subject   st: Using foreach to loop over macros
Date   Tue, 22 Aug 2006 14:16:12 -0400


I wondered if anyone could help me find a more efficient way to run
some very repetitive commands. I'm trying to create multiple versions
of several new variables using the egen functions. Since each version
of the new variables is based on the same set of original variables,
but the sets differ for each cluster of versions, I've been using local
macros to simplify matters:

local nv1 = "v1 v3 v5 v11"
local nv2 = "v2 v4 v9 v10"
local nv9 = "v79 v81 v82 v108"

I had thought that perhaps the egen commands could then loop over each
macro, with the names of the new variables containing some reference to
the macro (so that I can see easily which original variables were
included in its calculation). I've tried several versions of the

foreach list of local nv* {
   egen "rowsum`list'" = rowtotal(`list')
   egen "rowmean`list'" = rowtotal(`list')
   egen "dumvar`list'" = anymatch(`list'), values (1/5)

Unfortunately, I've been unable to get this to work. The most common
error message that I've encountered is that STATA requries "}" ... even
though I've included it each time.

Any advice/ideas would be appreciated. Is it just my syntax that is
flawed or is my basic approach also at fault?


Walter Forrest


----- End forwarded message -----


*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index