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

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

----- Original Message ----- From: "Nick Cox" <>
To: <>
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.


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 <> 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
How to interprete this?
I guess there is better method to achieve the goal. Any hint will be

*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2022 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index