Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: RE: foreach and levels of string variable
From
Nick Cox <[email protected]>
To
Nick Cox <[email protected]>, "'[email protected]'" <[email protected]>
Subject
st: RE: foreach and levels of string variable
Date
Mon, 27 Sep 2010 22:19:27 +0100
I got it. You are now being bitten by an old misfeature. See another Stata FAQ:
Why does a program defined by an ado-file sometimes not work when given a string comparison?
<http://www.stata.com/support/faqs/lang/define.html>
Note that you should have spelled out, as requested by the FAQ, that you were using a user-written program. -ptrend- on SSC is written for Stata 4.
Nick
[email protected]
Nick Cox
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?
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 <[email protected]> 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/