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

st: RE: looping with macro names


From   "Nick Winter" <[email protected]>
To   <[email protected]>
Subject   st: RE: looping with macro names
Date   Thu, 27 Mar 2003 11:34:25 -0500

> -----Original Message-----
> From: Chris Rohlfs [mailto:[email protected]] 
> Sent: Thursday, March 27, 2003 11:09 AM
> To: [email protected]
> Subject: st: looping with macro names
> 
> 
> hi!
> 
> i'm trying to make a loop to run statements like:
> 
> 	qui gen `mathXfirst' = math*first
> 	qui gen `englishXfirst' = english*first
> 	qui gen `other_acadXfirst' = other_acad*first
> 
> 	qui gen `mathXst' = math*st
> 	qui gen `englishXst' = english*st
> 	qui gen `other_acadXst' = other_acad*st
> 
> where the first variable in each case is a tempvar name and 
> hence inside
> quotation marks.  is there a way to do something like:
> 
> 	for C in var math english other_acad: for V in var first st: ...
> 
> or something with foreach that will replace the V and C with variable
> names, even inside the quotation marks?
> 
> thanks!
> chris

I think you want:

	foreach C in math english other_acad {
	  foreach V in first st {
	    qui gen ``C'X`V'' = `C'*`V'
	  }
	}


The first time through, for example, Stata expands the `C' to be -math-,
and the `V' to be -first-, so ``C'X`V'' expands to `mathXfirst', which
then expands to the tempvar of that name.

I'm assuming here that the tempvars have been declared already, but if
not, you can do it as you go:

	foreach C in math english other_acad {
	  foreach V in first st {
	    tempvar `C'X`V'
	    qui gen ``C'X`V'' = `C'*`V'
	  }
	}


NIck WInter

*
*   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