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

Re: st: loop that refers to global

From   David Kantor <>
Subject   Re: st: loop that refers to global
Date   Tue, 24 Jun 2003 18:47:18 -0400

At 03:03 PM 6/24/2003 -0700, Bogdan Savyich wrote:
I want to write a do-file that uses loops to refer to some global macros.
Global macros define different variables that I want to use in different
regressions.  However, when I run program I receive an error.  Example of
the do-file is goes like this:

version 7
global cov1="var3"
global cov2="var3 var4"
global cov3="var3 var4 var5"

local i=1
while `i'<=3 {
        logistic y var1 var2 $cov`i'
        local i=`i'+1

After I run this do file it gives an error:

1 invalid name

I think that what you need is to put braces in the appropriate places. Try this:
logistic y var1 var2 ${cov`i' }

Braces force the order of macro binding. Without them, Stata tries to first decode $cov -- which is nothing. Then it decodes `i' which is "1". That's the source of your error. The braces will force "cov`i'" to be decoded first, which is what you want.

-- David

David Kantor
Institute for Policy Studies
Johns Hopkins University

* For searches and help try:

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