Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: converting macros into variables


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: converting macros into variables
Date   Fri, 13 Feb 2009 12:35:51 -0000

Kieran McCaul pointed in the right direction. 

You could do this to get the macros into numeric variables: 

local x1 = 1
local x2 = 2
foreach v in x1 x2 {
        gen `v' = ``v''
}

Notice that I cut a line. 

The loop would be different for string variables: 

foreach v in x1 x2{
        gen `v' = "``v''"
}

The key point is that your macros are on two levels. 

The macro v contains in turn x1 and x2. 

The macros x1 and x2 contain 1 and 2 respectively. 

In fact in your example you could do it otherwise 

foreach v in 1 2 { 
	gen x`v' = `v' 
}

But Martin Weiss's question still stands. Why do you want do this? It
would be usually be inefficient as well as unnecessary. 

Nick 
[email protected] 

Luhang Wang

Does anyone know an easy way of converting macros into variables?
I'm trying something like the following.
local x1=1
local x2=2
local vl "x1 x2"
foreach v of loc vl {
        gen "`v'"=`"`v'"'
}
I end up with error message
"x1 invalid name
r(198);
How to interprete this?
I guess there is better method to achieve the goal. Any hint will be
appreciated.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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