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

st: RE: RE: macro basics (sequel)


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: macro basics (sequel)
Date   Fri, 26 Jul 2002 09:41:18 +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.

There is a sequel. It could be, 
as Bill Gould reminded me the other day, 
that this kind of problem is better tackled 
with a -reshape-. 

Your data are wide, in -reshape-'s jargon. 

If you -reshape- to long, something like 

reshape long air_reg voc_reg <others> , i(id) 

then calculate a product just once 

gen p_air_reg_voc_reg = air_reg * voc_reg 

and then the sum of the products 

bysort id : egen double sp_air_reg_voc_reg = sum(p_air_reg_voc_reg) 

The advantage of this is that other sums of products 
are equally easy. 

Once this is done, -reshape- back again. 

For more details, see [R] reshape and, if need be, 

http://www.stata.com/support/faqs/data/reshape3.html

Nick 
n.j.cox@durham.ac.uk 

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index