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

RE: st: using different macros everytime

From   "Nick Cox" <>
To   <>
Subject   RE: st: using different macros everytime
Date   Mon, 16 Jun 2008 10:15:22 +0100

Going back to your original example: 

local xset `" "x1 x2" "x1 x2 x3" "' 
local zset `" "x1 x2 v1" "x1 x2 x3 v1" "' 

* start of stuff for program or do file only 

local Tx : word count `xset' 
local Tz : word count `zset' 

if `Tx' != `Tz' { 
	di as err "no go; numbers of x sets and z sets do not match"
	exit 498 

* end of stuff for program or do file only 

forval t = 1/`T' { 
	local x : word `t' of `xset' 
	local z : word `t' of `zset' 
	treatreg y1 `x', treat(y2 = `z') 

I can't see much advantage in doing it this way unless the number of
sets is much larger than in the example. 


Shehzad Ali

Your solution makes good sense. With apologies, can I ask for a simple
clarification here? I didn't completely understand - forval - loop part
the solution. I understand that we define local T which captures no. of
and then forval loops each one of them one after another but I am not
how I use my treatreg commands (say for instance I only wanted to run a
simple treatreg command). 

Nick Cox

On the major point, you can bind variable names into groups using double
quotes " ". This follows from the syntax that double quotes bind more
tightly than spaces separate. 

Thus you could go 

local xset `" "a b c" "d e f" "g h j" "' 
local zset `" "A B C" "D E F" "G H J" "' 

Note the use of compound double quotes in defining the whole. Then 

local T : word count `xset'

forval t = 1/`T' { 

	[stuff referring to word `t' of `xset'] 

	[stuff referring to word `t' of `zset'] 


I'm all for improving one's mastery of Stata technique, but sometimes
it's just quicker to crunch through the variations of commands using a
do file and a decent text editor -- especially if you may spend much
time later trying to work out what was that clever thing you did. 

Incidentally, the approach here of replacing variables by their means
and then reinstating them is quite unnecessary and indeed I would
classify it as very bad style. Never touch originals you care about.
Rather -generate- new variables with the means concerned. Or, something
more like this: 

gen work1 = . 
gen work2 = . 

within a loop { 

	su <something>, meanonly 
	replace work1 = r(mean) 

	su <otherthing>, meanonly 
	replace work2 = r(mean) 


Shehzad Ali

Let me write down a few details about the analysis I am
trying to run. Please ignore if some of the details are irrelevant. I am
trying to run what they call 'standardisation' in literature. So here is
what I intend to do.

First loop:

1. run treatreg model: treatreg y1 x1 x2 x3 x4 x5, treat(y2=x1 x2 x3 x4

2. then set x1 x2 at their mean values (first generate copies of x1 x2,
x1_copy x2_copy, then summarize original x1 x2 and replace them with
mean values). 

3.then predict yhat (so basically it uses coefficients from treatreg but
x2 are at their mean values and others at their actual values)

4. drop x1 x2 (which were set at their mean values) and then rename
and x2_copy as x1 x2 (so these variables are back as original)

Second loop:

4. Run the same as above but this time setting x2 x4 x5 at their mean

Going on further, I have a few more loops where the variables which are
to their mean values are different (no pattern of selection of

What I was thinking of doing was to define sets of macros, i.e. for
loop, define macro1 as variables to be set at their mean value and
macro2 as
variables to be left as original. For second loop, I do the same and it
on until I have run all the loops I wanted to run. What I needed help
is how to assign names to each macro and then I can run a foreach loop
that the macros are defined at the start and then treatreg and the rest
run as a loop several times.

Maarten buis

It is usually better practice to work with locals. Anyhow the answer to
your question depends on the kind of pattern you want those models to
follow. Do you want something like

x1 x2 x3
x1 x2 x4
x1 x2 x5


x1 x2 x3
x1 x2 x3 x4
x1 x2 x3 x4 x5


all combinations.

Moreover, do these variables have such systematic names, or do they
have more meaningful, but less systematic names?

Shehzad Ali 

> Sorry, Martin, it was a mistake. I actually meant global. The models
> I am
> currently using  are:
> * First model:
> global xvar "x1 x2" 
> global zvar "x1 x2 v1"
> treatreg y1 $xvars, treat(y2=$zvars)
> * Second model:
> global xvar "x2 x3 x4"  
> global zvar "x2 x3 x4 v1"
> treatreg y1 $xvars, treat(y2=$zvars)
> Note that I am not just adding one or two additional variables in the
> second
> model, but I am actually using a different set of variables the
> second time.
> Now I want to loop this somehow so that I just write the - treatreg -
> command line once and - foreach - can pick up different sets of
> macros each
> time the loop is run. Can you please suggest the best way to do it?
Martin Weiss
> Just a remark: are you sure you want to reference a -local- with the
> $  
> sign? I do not think that this will actually work...
Shehzad Ali
> > I want to run a 2 separate treatreg models with the same y var but
> > different set of x vars. Here is what I am doing:
> >
> > * First model:
> >
> > local xvar "x1 x2"  local zvar "x1 x2 v1"
> >
> > treatreg y1 $xvars, treat(y2=$zvars)
> >
> > * Second model:
> >
> > local xvar "x1 x2 x3"  local zvar "x1 x2 x3 v1"
> >
> > treatreg y1 $xvars, treat(y2=$zvars)
> >
> > Now I want to loop this somehow so that I just write the - treatreg
> -
> > line once but - foreach - can pick up different sets of macros. Can
> you
> > suggest the best way to do it?

*   For searches and help try:

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