Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Strange problem with stcox inside a loop


From   Allen Buxton <[email protected]>
To   [email protected]
Subject   Re: st: Strange problem with stcox inside a loop
Date   Sat, 16 Jun 2007 08:44:20 -0700

This is interesting, on the use of macros - sometimes it helps me to display (di) a macro.
thanks,
-Allen Buxton
===
sysuse cancer
stset studytime, fail(died)
xi i.drug
local model1 `"_Idrug* "'
local model2 `"_Idrug* age "'
local models `"`model1' `model2'"'
local options `" nolog"'
foreach m in `models' {
di `"stcox `m' ,`options'"'
stcox `m' ,`options'
foreach var in `m' {
testparm `var'
}
}
===
K Jensen wrote:

Thanks for replying, Maarten.

Unfortunately, the solution you posted runs  _Idrug* and age as
separate models, which wasn't what I intended, and was why I used the
double quotes as I did.

The results on the analogous code for regress on the auto dataset that
I posted before do work, and show what I was trying to do.

With best wishes and thanks
Karin

On 16/06/07, Maarten buis wrote:
--- K Jensen <[email protected]> wrote:
I am trying to use some foreach loops to run various stcox models
and to use testparm on them.  I have a problem on the second run
on each loop.  What I'm doing is more complex, but the following
code on the system "cancer" dataset causes the same problem:

sysuse cancer
stset studytime, fail(died)
xi i.drug
local model1 ""_Idrug*""
local model2 ""_Idrug* age""
local models "`model1'" "`model2'"
local options ", nolog"
foreach m in `models' {
  stcox `m' `options'
  foreach var in `m' {
      testparm `var'
  }
}
This works fine for model1, which produces results as expected,
but on model2 it generates the error message:
"option nolog not allowed"
The problem has to do with the way you use double quotes. It's use
is explained in -help quotes-. An example that works is shown
below:

*------------- begin example ---------------
sysuse cancer, clear
stset studytime, fail(died)
xi i.drug
local model1 "_Idrug*"
local model2 "_Idrug* age"
local models `"`model1' `model2'"'
local options ", nolog"

foreach m in `models' {
 stcox `m' `options'
 foreach var in `m' {
     testparm `var'
 }
}
*-------------- end example ----------------

Hope this helps,
Maarten

I don't think it's a function of the way I have written the loop,
as an analogous loop with the regress command works perfectly
well:
sysuse auto
xi i.rep78
local model1 ""_Irep78*""
local model2 ""_Irep78* weight""
local models "`model1'" "`model2'"
local options ", noheader"
foreach m in `models' {
  regress price `m' `options'
  foreach var in `m' {
      testparm `var'
  }
}

Can anybody explain why this is happening?

Thankyou
Karin
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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