Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: strange problem


From   "Anthony Gichangi" <[email protected]>
To   <[email protected]>
Subject   Re: st: strange problem
Date   Fri, 2 Jul 2004 10:36:58 +0200

There was more than 200 observations per drug. Now I understand why it was
not working. When i encoded drugs they were assigned 1,2,3,..... However
when
I keep the first observation per person not all drugs appear in the first
observations.
The levels command enabled me to see that ! thanks uli!

With kind Regards
Anthony

----- Original Message ----- 
From: "Ulrich Kohler" <[email protected]>
To: <[email protected]>
Sent: Friday, July 02, 2004 9:30 AM
Subject: Re: st: strange problem


> Anthony Gichangi wrote:
> > I am trying to run the following code on my dataset but I get
> > a strange error which I  don't understand, any ideas
> >
> > CODE:
> >
> > des drug fmtab
> > set trace on
> > qui sum drug
> > preserve
> > bysort prn : keep if _n==1
> > forvalues x = 1(1)`r(max)' {
> > qui sum fmtab if drug ==`x', de
> > local med`x' = `r(p50)'
> > di `med`x''
> > }
> > restore
> >
> >
> > RESULTS:
> >
> >
> > des drug fmtab
> >
> >               storage  display     value
> > variable name   type   format      label      variable label
>
> --------------------------------------------------------------------------
-
> >- ---
> > drug            long   %15.0g      drugc
> > fmtab           float  %9.0g
> >
> > . set trace on
> > . qui sum drug
> > . preserve
> > . bysort prn : keep if _n==1
> > (296782 observations deleted)
> > . forvalues x = 1(1)`r(max)' {
> >   2. qui  sum fmtab if drug ==`x', de
> >   3. local   med`x' =   `r(p50)'
> >   4.
> > . di `med`x''
> >   5. }
> > - forvalues x = 1(1)`r(max)' {
> > = forvalues x = 1(1)25 {
> > - qui sum fmtab if drug ==`x', de
> > = qui sum fmtab if drug ==1, de
> > - local med`x' = `r(p50)'
> > = local med1 = .3703703582286835
> > - di `med`x''
> > = di .3703703582286835
> > .37037036
> > - }
> > - qui sum fmtab if drug ==`x', de
> > = qui sum fmtab if drug ==2, de
> > - local med`x' = `r(p50)'
> > = local med2 =
> > invalid syntax
> >   di `med`x''
> >   }
> >
> > Strange enough,  the median for drug 2 is not missing because i have
over
> > 200 observations
>
> Are you sure to have 200 observations even you dropped 296782
observations?
> Maybe you should leave out -quietly- in front of -sum fmtab if drug ==`x'-
to
> see what's going on.
>
> Another point is, that you are more save if you use -levels- to set up
your
> loop. And you should drop the observations _before_ you determine the end
of
> the loop. I propose the following:
>
> preserve
> bysort prn : keep if _n==1
> levels drug, local(K)
>   foreach k of local K {
>   _pctile famtab if drug == `k', percentiles(50)
>   di r(r1)
> }
> restore
>
> regards
> uli
>
> -- 
> [email protected]
> +49 (030) 25491-361
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index