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]
Re: st: RE: foreach and levels of string variable
From
"Paul O'Brien" <[email protected]>
To
[email protected]
Subject
Re: st: RE: foreach and levels of string variable
Date
Tue, 28 Sep 2010 08:43:44 +0100
thanks nick,
this is getting a bit complicated for me.
to avoid the problem you refer to, i could, of course, do a simple logistic on each study.
but it does not seem to recognise the foreach, just repeats the same analysis of the whole dataset.
. levelsof study, local(levelstud)
`"Creinin 2006"' `"Glasier 2010"' `"Hamoda 2004"' `"Ho&Kwan 1993"' `"WHO 1998"' `"von Hertzen 2002"'
. foreach l of local levelstud {
2. logistic p day[fw=pop]
3. }
Logistic regression Number of obs = 6755
LR chi2(1) = 4.34
Prob > chi2 = 0.0373
Log likelihood = -604.4323 Pseudo R2 = 0.0036
------------------------------------------------------------------------------
p | Odds Ratio Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
day | 1.198313 .1013991 2.14 0.033 1.01518 1.414482
------------------------------------------------------------------------------
Logistic regression Number of obs = 6755
LR chi2(1) = 4.34
Prob > chi2 = 0.0373
paul
On 27 Sep 2010, at 22:27, Nick Cox wrote:
> Here's a fix for you (better than that in the FAQ cited just below):
>
> egen group = group(study), label
> su group, meanonly
>
> forval i = 1/`r(max)' {
> di "`: label (group) `i''"
> ptrend p1 np1 day if group == `i'
> }
>
> This strategy is covered by the FAQ you cited at the beginning of this thread.
>
> Nick
> [email protected]
>
> Nick Cox
>
> 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/
-------------------------------
Dr Paul O'Brien
Raymede Clinic
Westside Contraceptive Services
Central London Community Healthcare
Exmoor St
London
W10 6DZ
UK
+44 (0)20 8962 4450
*
* 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/