Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

st: RE: Modifying the content of a macro


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Modifying the content of a macro
Date   Wed, 28 Jul 2010 20:18:27 +0100

If I understand you correctly, you want something more like this: 

sysuse auto
local main mpg price weight
local adjust length gear_ratio turn
local length length 
local nolength : list adjust - length

foreach c of local main {
	if "`c'" != "weight"  logistic foreign `c' `adjust'
	else logistic foreign `c' `nolength' 
}

Key points: 

1. Take outside the loop what does not need to be done inside. 

2. The stuff at -help macrolist directives- requires macro names as
arguments. That excludes explicit strings. 

3. Sort out the difference between -if- and -if-: see 

FAQ     . . . . . . . . . . . . . . . . . . . . .  if command vs. if
qualifier
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  J.
Wernow
        6/00    I have an if or while command in my program that
                only seems to evaluate the first observation,
                what's going on?
                http://www.stata.com/support/faqs/lang/ifqualifier.html

Of course, if the last three commands are really all you want, doing it
this way just makes life more difficult. 

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

Thomas Speidel

I am writing a loop to cycle through a number of models. For example,

. sysuse auto
(1978 Automobile Data)

. local main mpg price weight

. local adjust length gear_ratio turn

. foreach c of local main {
   2. local nolength: list adjust - "length"
   3. logistic foreign `c' `adjust' if "`c'" != "weight"
   4. logistic foreign `c' `nolength' if "`c'" == "weight"
   5. }

Which fails on the second model.  How do I modify `adjust' so that the  
string "length" is removed from its content and applied to the second  
set of models? In a less automated fashion, I want:

. logistic foreign mpg length gear_ratio turn
. logistic foreign price length gear_ratio turn
. logistic foreign weight gear_ratio turn

Preferrably, in this order.  Creating another macro without "length"  
is not ideal, because I am dealing with a much larger set of covariates.
Thanks.


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index