I have to generate a new variable (adjusted_pri) for each country (cname) that is the replace of an existing variable (primary_income) with values 10 times the median for those at the top of the distribution and values equal the 1% of the equivalized income (equ_primary) for those at the bottom. Of course these values are changing according to the waves in which the country distribution is observed.
Given the if-option is not allowed in the local, what I thought is the following code:
Code:
levelsof cname, local(c) clean levels wave_1, local(w) foreach cname in `c' { foreach wave_1 in `w' { sum primary_income [aw=hpowgt] if cname=="`cname'" & wave_1==`wave_1' local top_pri "`cname'"_`wave_1'=10*r(p50) sum equ_primary [aw=hpowgt] if cname=="`cname'" & wave_1==`wave_1' local bottom_pri "`cname'"_`wave_1' =r(mean)/100 gen adjusted_primary= primary_income replace adjusted_primary=$top_pri "`cname'"_`wave_1' if primary_income>top_pri "`cname'"_`wave_1' & "`cname'" & wave_1==`wave_1' replace adjusted_primary=$bottom_pri "`cname'"_`wave_1' if primary_income<=0 & "`cname'" & wave_1==`wave_1'
Can you please help me in found a solution for this loop?
Thanks,
Luca
0 Response to Local with conditions within a loop
Post a Comment