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/