Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <n.j.cox@durham.ac.uk> |
To | "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu> |
Subject | st: RE: foreach and levels of string variable |
Date | Mon, 27 Sep 2010 22:12:06 +0100 |
This works for me: . clear . set obs 10 obs was 0, now 10 . gen which = cond(_n < 6, "some stuff", "other stuff") . gen y = _n . levelsof which, local(levels) `"other stuff"' `"some stuff"' . foreach l of local levels { 2. su y if which == "`l'" 3. } Variable | Obs Mean Std. Dev. Min Max -------------+-------------------------------------------------------- y | 5 8 1.581139 6 10 Variable | Obs Mean Std. Dev. Min Max -------------+-------------------------------------------------------- y | 5 3 1.581139 1 5 I can't see what you're doing wrong. The code looks right. Are you doing something weird like running code from a do-file editor and the interactive session in combination? Nick n.j.cox@durham.ac.uk paul o'brien thanks martin and neil with the advice on adding quotes. however, i still get the same error: . 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); removed " 2002" in "Ashok 2002" and others - same problem. On 27 September 2010 11:42, Maarten buis <maartenbuis@yahoo.co.uk> wrote: > > --- On Mon, 27/9/10, Paul O'Brien wrote: > > . levelsof study, local(levelstud) > > > > . foreach l of local levelstud { > > 2. ptrend p1 np1 day if study == `l' > > 3. } > > Ashok not found > > r(111); > > > > where am i going wrong? > > ptrend p1 np1 day if study == `l' > > should be > > ptrend p1 np1 day if study == "`l'" > * * 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/