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

RE: st: Adding new variables to an existing global macro listof variables.


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Adding new variables to an existing global macro listof variables.
Date   Fri, 6 Aug 2004 18:38:21 +0100

use "C:\XYZ.dta",clear
global Xlist
 
foreach var of varlist lntoen lncap lntech lndebt {
	sum `var'
	gen mean`var' = r(mean)
	global Xlist "$Xlist mean`var'" 
}

egen Y=min($Xlist)

But you don't need to use a global for this. Nor
need you store single values in variables. 

use "C:\XYZ.dta",clear
local mean = .  
 
foreach var of varlist lntoen lncap lntech lndebt {
	su `var', meanonly 
	if r(mean) < `mean' { 
		local lowest "`var'" 
		local mean = r(mean)
	} 
}

di "`var'" `mean' 

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

Dev Vencappa

>  is there a way to create a global list of variables that 
> starts empty and successively adds new variables to the list 
> as they are created? I am trying to write something like:
> 
> use "C:\XYZ.dta",clear
> 
> global Xlist
> 
> foreach var of varlist lntoen lncap lntech lndebt{
> sum `var'
> gen mean`var'=r(mean)
> global (add the newly created variable mean`var' to $Xlist)
> }
> 
> *then take the minimum of all these variables
> egen Y=min($Xlist)
> 
> The reason I am trying to use a macro is because I have to 
> create many variables and use the one that has the lowest 
> value. This has to be done within a loop for each of several years.
> 
> Any alternative approach to mine would be really appreciated.

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