Dear All,
I have posted a question with regard to looping, but I confess my question is vegue.
Let me try with a simple one.
I have a data that looks like this:
Now I want to replace the missing value of variable price based on regional average
I did the following:
Keep if crop ==1
gen work = .
sort r1_region
foreach v of var cr_priceperKG {
by r1_region: replace work = sum(`v') / sum(`v' < .)
by r1_region: replace work = work[_N]
replace `v' = work if `v'==.
drop work
}
}
This replaced the missing values of price based on regional average.
I want to extend this to include many types of crops at once instead of keeping crop number x and drop others.
Thanks for the help
Sirak
I have posted a question with regard to looping, but I confess my question is vegue.
Let me try with a simple one.
I have a data that looks like this:
Region | crop | production | value sold | quantity sold | Price |
1 | 1 | 30 | 34.2 | 20 | 1.71 |
1 | 1 | 44 | 0 | 0 | . |
1 | 1 | 25 | 0 | 0 | . |
1 | 1 | 63 | 98.05 | 53 | 1.85 |
2 | 1 | 96 | 206.4 | 86 | 2.4 |
2 | 1 | 25 | 30 | 15 | 2 |
2 | 1 | 14 | 0 | 0 | . |
2 | 1 | 12 | 0 | 0 | . |
3 | 1 | 13 | 5.7 | 3 | 1.9 |
3 | 1 | 65 | 0 | 0 | . |
3 | 1 | 14 | 5.8 | 4 | 1.45 |
3 | 1 | 54 | 70.4 | 44 | 1.6 |
4 | 1 | 58 | 0 | 0 | . |
4 | 1 | 74 | 36 | 30 | 1.2 |
4 | 1 | 62 | 0 | 0 | . |
4 | 1 | 35 | 32.5 | 25 | 1.3 |
I did the following:
Keep if crop ==1
gen work = .
sort r1_region
foreach v of var cr_priceperKG {
by r1_region: replace work = sum(`v') / sum(`v' < .)
by r1_region: replace work = work[_N]
replace `v' = work if `v'==.
drop work
}
}
This replaced the missing values of price based on regional average.
I want to extend this to include many types of crops at once instead of keeping crop number x and drop others.
Thanks for the help
Sirak
Comment