The Statalist FAQ advises that you make clear when
you are using non-official commands, in this case -levels7-
and -povdeco-, and where they come from. This advice has also
been flagged within this list very recently.
There is no reason why you should use
-levels7- when you have Stata 9. -levels7- is a user-written
program that is part of the SSC package -levels-. But as
its name implies, and as its documentation makes clear, it
is for users of Stata 7.
The history is a little complicated, for which I am largely
responsible, but not difficult to understand.
(a) The name -levels- was given to a program I wrote which
replaced an earlier program called -vallist-. This was
distributed via SSC. When I wrote a version that required
Stata 8, a version -levels7- was provided for those who
were still on 7 and wished to use it.
(b) Within the lifetime of Stata 8, StataCorp adopted -levels-
as an official command. (And it's still there, as -levels-.)
(c) StataCorp for Stata 9 had second thoughts about the name, so it's
now overtly -levelsof-.
That's all by the by, as there is no evidence here for any problem with -levels7-.
I'd rewrite your code as
gen P0muni = .
gen P1muni = .
gen P2muni = .
local pline = pline
egen var = group(year muni)
povdeco iewb [w=expc], by(var) pline(`pline')
levelsof var, local (S)
qui foreach r of local S {
replace P0muni=r(fgt0_`r') if var == `r'
replace P1muni=r(fgt1_`r') if var == `r'
replace P2muni=r(fgt2_`r') if var == `r'
}
drop var
save, replace
but that is subject to confirmation by those who
use -povdeco- and I can't explain why results are
not as you expect. -povdeco-'s author, Stephen Jenkins, is a member
of Statalist. Note how you shouldn't need to repeat -povdeco-
within the loop.
Your line
local pline = pline
only makes sense on the assumption that you have
a variable -pline- which contains your value of -pline-,
so that
local pline = pline
is really a shorthand way of
saying
local pline = pline[1]
This does what you want, but such practice will bite
you sometime later unless you understand what you are
doing. It will also make code difficult to understand
later unless you understand what you are doing. It's
much simpler not to use a variable to store a constant
and just to use an explicit call such as -pline(2000)-.
In this case, -levelsof- can be dispensed with:
gen P0muni = .
gen P1muni = .
gen P2muni = .
local pline = pline
egen var = group(year muni)
povdeco iewb [w=expc], by(var) pline(`pline')
su var, meanonly
qui forval r = 1/`r(max)' {
replace P0muni = r(fgt0_`r') if var == `r'
replace P1muni = r(fgt1_`r') if var == `r'
replace P2muni = r(fgt2_`r') if var == `r'
}
drop var
save, replace
but it does no harm.
Nick
[email protected]
Pol Smith
> I am having trouble with povdeco and/or levels7 in stata v. 9
> - i suspect the latter. I have a dataset in which the dataset
> is classified by year and municipality, and I would like to
> generate municipal poverty rates for each year. However, when
> I run the program below, I get perfect results for the first
> year, then for each successive year the poverty rates for
> each municipality are far lower than they *should* be.
> I'd be extremely grateful for your help.
>
> Stata code:
> gen P0muni=.
> gen P1muni=.
> gen P2muni=.
> local pline=pline
> egen var=group(year muni)
> levels7 var, local (S)
> foreach r of local S {
> povdeco iewb [w=expc], by(var) pl(`pline')
> replace P0muni=r(fgt0_`r') if var == `r'
> replace P1muni=r(fgt1_`r') if var == `r'
> replace P2muni=r(fgt2_`r') if var == `r'
> }
> drop var
> save, replace
*
* 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/