Statalist The Stata Listserver


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

Re: st: The Mystery Remains (was: Strange problem with stcox inside a loop)


From   Maarten buis <[email protected]>
To   [email protected]
Subject   Re: st: The Mystery Remains (was: Strange problem with stcox inside a loop)
Date   Sun, 17 Jun 2007 12:34:49 +0100 (BST)

If you type  -di `"`models'"'- before the beginning of the loop you can
see the content of the local:

"_Idrug*"" ""_Idrug* age"

The local contains three strings:
1. "_Idrug*"
2. " "
3. "_Idrug* age"

So the second model that you try to estimate is:
stcox  , nocons
and this is not allowed.

Basically you want to separate models by surrounding them by quotes, so
your list containing all models must contain quotes. That list is
itself a string. In Stata (and many other languages) strings are
themselves surrounded by quotes, so you must tell Stata which quote is
part of the string (delimitating models) and which quote means end of
string. The way to do that is to suround that string with compound
quotes `" and "'. Anyting between `" and "' is part of the string
including any ". This way Stata can discern between the beginning and
end of the string and the " that is part of the string. You can read
more about this in -help quotes-. 

A working example is below. Notice the way compound quotes are used to
indicate that " is part of the string:
*------------- 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"
di `"`models'"'
foreach m in `models' {
  stcox `m' `options'
  foreach var in `m' {
      testparm `var'
  }
}
*-------------- end example ----------------

Hope this helps,
Maarten

Ps. be careful with using all captial letters when posting to mailing
lists. This is generally seen as the equivalent of shouting and is very
rarely appropriate. 

--- K Jensen <[email protected]> wrote:

> Thanks for the -tuples- suggestion, Maarten.  It looks like a useful
> command in other contexts, but I don't want to run all possible
> combinations of variables.
> 
> So, the mystery remains.  Can ANYONE out there explain why the code
> marked "A" below works, but the code "B" gets the error message
> "option nolog not allowed" when it runs model2?  Please!?
> 
> Yours desperately
> Karin
> 
> *------------- begin A ---------------
> 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'
>   }
> }
> *-------------- end A ----------------
> 
> *------------- begin B ---------------
> 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'
>   }
> }
> *-------------- end B ----------------
> 
> On 16/06/07, Maarten buis <[email protected]> wrote:
> > what about Nick's -tuples- command? See: -ssc desc tuples-
> >
> > Hope this helps,
> > Maarten
> >
> > --- K Jensen <[email protected]> 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:
> >>> 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
> *
> *   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/
> 


-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------


      ___________________________________________________________ 
Yahoo! Mail is the world's favourite email. Don't settle for less, sign up for
your free account today http://uk.rd.yahoo.com/evt=44106/*http://uk.docs.yahoo.com/mail/winter07.html 
*
*   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