Thanks for all replies. Nick's solution works!
The circumstance is that my statistics are based on summaries of
different sub-samples and I store them in macros along the way. The
final product I want is a table showing all the statistics I
construct, thus I just want to convert the macros I've kept into
numeric variables.
I know -collapse- provides a way of going from observations to
statistics, but it doesn't seem to be more efficient, or at least
feels less intuitive to me, when I need to work across sub-samples.
Again, thanks for all the help.
Best,
Luhang
On Fri, Feb 13, 2009 at 7:35 AM, Nick Cox <[email protected]> wrote:
> 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/
>
*
* 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/