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

st: RE: Creating a macro of variable names


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Creating a macro of variable names
Date   Fri, 13 May 2005 17:38:10 +0100

(1) 
unab varlist : abc* 
global varlist "`varlist'" 

(2) 
ds abc* 
global varlist "`r(varlist)'" 

Your loop method should work with 

(3) 
foreach var of varlist abc* {
	global varmacro "$varmacro `var'"
}

but the first method above is better. 
The second method is better when you need
the options of -ds-. 

There may be a better one yet that 
I've forgotten at this instant. 
 
Nick 
[email protected] 

David Hadley
 
> I'm trying to create a global macro containing certain 
> variable names - 
> but I'm having trouble appending/concatenating macros (as opposed to 
> redefining the macro).
> 
> Assuming I have some variables called abc1, abc2, and abc3, here's my 
> do-file so far:
> 
> foreach var of varlist abc* {
> 	global varmacro = `"`$varmacro' `var'"'
> }
> 
> This currently only stores (in varmacro) the last abc* variable 
> ("abc3"), whereas I want it to produce a macro of "abc1 abc2 abc3".
> 
> I would have though this command could be done in one line 
> rather than 
> using a do-file, but would appreciate any method that works.

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