Im using Stata 12.1, my dataset has around 50000 participants, each participant has a village code, the villages are grouped in predetermined clusters and I have a key to assign villages into their respective clusters. What i'd like to do is assign each participant their respective cluster based on what village code (variable: vill_no) they have.
Iv'e set up two local varlist one with each unique village_code and other with respective clusterID, the order they come in the lists is very important, ie the 1st villagecode, is assigned the 1st cluster ID.
The idea is to loop it using foreach, if the village code(vill_no) variable matches the local village code, to assign that partcipant the same ordered clusterID.
the script runs but is not making any changes or assigning any clusterIDs.
Anyone have ideas, it seems like a simple concept but Im stuck on the code.
Thanks!
-Ben
Code:
gen clustername="" local clusterID "BNBS BNBS BNBS BNST BNST BNST BNSB BNSB BNSB BNSB ...." local villagecode "64006 73015 73048 73059 73187 73216 73016 73049 73132 73188 73002 73020 .........." foreach x in `clusterID' & y in `villagecode'{ replace clustername="`x'" if vill_no=="`y" }
0 Response to How to use the foreach code with multiple varlists.
Post a Comment