This is my first post on statalist. I have a household survey data (119,018 observations). It has a 5-digit variable named "stratum" where the first digit is for the province and second and third digit is for district. Fourth digit is for 'region' whether rural or urban and last digit is for income group. This data set I want to ultimately merge with shapefile for which I need district id variable which I will use as a key variable to merge with shapefile. I have created this variable by recoding the stratum equivalent to district ids in shapefile. There are 161 districts.
I need another variable 'district name' for which I am using a loop but it does not return what I want. It assigns last district name to each observation. What I want is to have the name of each district in front of its code.
Following is the code I have used so far. Thanks in advance.
Code:
clear input long stratum 12410 12410 12410 21110 21110 25410 25410 25410 61121 61121 60010 end capture drop distt_id gen distt_id=stratum recode distt_id (12410=62) (21110=86) (25410=108) (61121 60010=54) label var distt_id "District ID" sort distt_id capture drop distt_name gen distt_name = "" local id 54 62 86 108 local name `" "Islamabad" "Abbottabad" "Attock" "Nankana" "' foreach i of local id { foreach v of local name { replace distt_name="`v'" if distt_id == `i' } }
0 Response to replacing values in a loop
Post a Comment