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]

Re: st: foreach and levels of string variable


From   Neil Shephard <nshephard@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: foreach and levels of string variable
Date   Mon, 27 Sep 2010 10:33:37 +0000

On Mon, Sep 27, 2010 at 10:28 AM, Paul O'Brien <paul.obrien@afe2.org.uk> wrote:
> hi,
>
> i have a dataset with results from a number clinical studies of efficacy of emergency contraception and want to test for trend by day's delay in treatment within each study.
>
> using nick cox's FAQ: Is there a way to tell Stata to try all values of a particular variable in a foreach statement without specifying them?
>
> . levelsof study, local(levelstud)
> `"Ashok 2002"' `"Creinin 2006"' `"Ellertson 2003"' `"Fine 2010"' `"Glasier 2010"' `"Hamoda 2004"' `"Ho&Kwan 1993"' `"WHO 19
>> 98"' `"von Hertzen 2002"'
>
> . foreach l of local levelstud {
>  2. ptrend p1 np1 day if study == `l'
>  3. }
> Ashok not found
> r(111);
>
> where am i going wrong?
>

You're missing double quotes around your reference to the local macro
`l'.  Try...

foreach l of local levelstud {
  ptrend p1 np1 day if study == "`l'"
}

Neil


-- 
"One should as a rule, respect public opinion in so far as is
necessary to avoid starvation and to keep out of prison, but anything
that goes beyond this is voluntary submission to an unnecessary
tyranny, and is likely to interfere with happiness in all kinds of
ways..." - Bertrand Russell

Email - nshephard@gmail.com
Website - http://slack.ser.man.ac.uk/
Photos - http://www.flickr.com/photos/slackline/

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