# Re: st: RE: converting macros into variables

 From "Martin Weiss" To Subject Re: st: RE: converting macros into variables Date Fri, 13 Feb 2009 20:30:36 +0100

```<>

"But without supply of more details, more details are difficult to supply. "

True, but -statsby- seems like a safe bet as a way forward for Luhang...

HTH
Martin
_______________________
```
----- Original Message ----- From: "Nick Cox" <n.j.cox@durham.ac.uk>
```To: <statalist@hsphsun2.harvard.edu>
Sent: Friday, February 13, 2009 7:44 PM
Subject: RE: st: RE: converting macros into variables

```
```>From what you say, there'll be an even better way to do it
without macros even, and certainly without putting (single) macros into
variables, But without supply of more details, more details are
difficult to supply.

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

Luhang Wang

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.

On Fri, Feb 13, 2009 at 7:35 AM, Nick Cox <n.j.cox@durham.ac.uk> 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.
```
```
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/
```