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 on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: foreach and levels of string variable


From   Nick Cox <n.j.cox@durham.ac.uk>
To   Nick Cox <n.j.cox@durham.ac.uk>, "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
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 
n.j.cox@durham.ac.uk 

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


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index