Hi everyone,
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'
It returns "Australia_2 not found", where Australia is the first country in the dataset. But it should be only the name of the local to be differentiated by each country and wave, as it is not allowed to write local top= 10*r(p50) if if cname=="`cname'" & wave_1==`wave_1'.

Can you please help me in found a solution for this loop?

Thanks,

Luca