Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: macro basics


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: macro basics
Date   Fri, 26 Jul 2002 09:14:54 +0100

Chris Rohlfs
> 
> i'm trying to figure out the macro basics & can't seem to 
> find any simple
> examples on the web or in the manual.  if someone could 
> please post the
> text of a simple *.do file that uses macros, i'd really 
> really appreciate
> it.  i'm using stata 7.
> 
> i'd also really appreciate any suggestions on my specific 
> problem that i'm
> trying to solve w/ macros:
> 
> to take variables (air_reg1, air_reg2, ..., air_reg24)
> and (voc_reg1, ..., voc_reg24)
> 
> and to produce the sum of the products:
> 
> air_reg1*voc_reg1 + air_reg2*voc_reg2 + ... + air_reg24*voc_reg24
> 
> for a bunch of different sets of variables ({air,voc} is 
> one example) w/ a
> large dataset.

The second half of [U] is the most useful 
section of the manual here. 

For your example: One way to do it is 

gen double sumproduct = 0 

qui forval i = 1/24 { 
	replace sumproduct = sumproduct + air_reg`i' * voc_reg`i'
} 

For one introduction to local macros as applied 
to working through lists, there is a tutorial 
in the Stata Journal 

"How to face lists with fortitude" 
Stata Journal 2(2):202--222 (2002) 

If you do not subscribe to the Journal, and 
naturally I recommend that, then the slides 
of a talk on this material are accessible 
at 

http://fmwww.bc.edu/RePEc/usug2002/fortitude.pdf

or at 

http://www.stata.com/support/meeting/8uk/fortitude.pdf

The recent thread on -foreach-, -forvalues- and -for- 
started by Elizabeth Simantov on Monday 22 with the 
heading 

st: create many variables at once

and continuing through to Kit Baum on Thursday 25 
with 

st: Re: for vs foreach and forvalues

is also relevant. 

-for- lovers would want me to point out 
that your problem can also be done this way: 

gen double sumproduct = 0 

qui for var air_reg* \ var voc_reg* : replace sumproduct = 
sumproduct + X * Y 

assuming that -air_reg1-air_reg24- and the 
corresponding -voc_reg*- are in that order in memory. 

Note that this -for- solution does not use local
macros. 

Nick 
[email protected] 

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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