Hi Statalisters! I am working on a dataset on school admissions that looks like this
schoolid is the code of the school that the child got admitted into (it also uniquely identifies all the schools) and p1-p7 are the preference variables (in my dataset I have p1-p178). I want to create a dummy variable for each school that takes a value 1 if the school is amongst the preferences and 0 otherwise.
I tried to generate school dummies with this code (suggested on this forum for a case where school codes don't start with numbers)
levelsof schoolid
display `r(levels)'
foreach v in `r(levels)' {
gen `v' = 0
quietly forval j = 1/178 {
replace `v' = 1 if p`j' == "`v'"
}
}
This code would work if the values of my schoolid and p* variables were not starting with numbers. Is there another way to generate dummies in this case? Or, is there a way to add a prefix to all the values in my schoolid and p* variables? If I can prefix an alphabet to every value, then the code above would create my dummies.
Many thanks!
Vijay
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input double id str7(schoolid p1 p2 p3 p4 p5 p6 p7) 20160000015 "1412148" "1412148" "1413329" "1413240" "1413245" "1413215" "1411253" "1411219" 20160000041 "." "1821202" "1821210" "1821232" "." "." "." "." 20160000053 "." "1617224" "1617166" "1617186" "1617204" "1617220" "1411253" "1617192" 20160000058 "." "1821202" "1821146" "1821210" "1618189" "." "." "." 20160000088 "." "1618249" "1821143" "1618232" "1514087" "1720147" "1514073" "1617192" 20160000090 "1720158" "1720158" "1720136" "1514085" "1514086" "1515105" "1516110" "1516113" 20160000097 "1105191" "1003262" "1002318" "1002294" "1001197" "1002306" "1003217" "1003225" 20160000106 "1617204" "1617204" "1617186" "1617192" "1617181" "1617229" "1618253" "1515114" 20160000107 "." "1617192" "1617204" "." "." "." "." "." 20160000115 "1413330" "1412257" "1412152" "1413330" "1413225" "1413227" "1413329" "1412148" 20160000122 "." "1617229" "1411253" "1617182" "1617186" "1413283" "1413183" "1413215" 20160000149 "1411219" "1411219" "." "." "." "." "." "." 20160000150 "." "1923255" "1720142" "1720145" "." "." "." "." 20160000157 "." "1413329" "1411253" "1617229" "." "." "." "." 20160000228 "." "1411183" "1411214" "1411201" "1207188" "1411199" "1207186" "1207181" 20160000264 "." "1617192" "1617229" "1617186" "1617182" "1617181" "1411253" "1514086" 20160000270 "." "1516116" "." "." "." "." "." "." 20160000276 "1104284" "1105208" "1106190" "1104301" "1105233" "1106218" "1104310" "1104275" 20160000283 "." "1411253" "1412135" "1412141" "1412151" "1412156" "1413205" "1413215" 20160000288 "1821173" "1821141" "1821232" "1821168" "1821145" "1821151" "1821152" "1821143" end
I tried to generate school dummies with this code (suggested on this forum for a case where school codes don't start with numbers)
levelsof schoolid
display `r(levels)'
foreach v in `r(levels)' {
gen `v' = 0
quietly forval j = 1/178 {
replace `v' = 1 if p`j' == "`v'"
}
}
This code would work if the values of my schoolid and p* variables were not starting with numbers. Is there another way to generate dummies in this case? Or, is there a way to add a prefix to all the values in my schoolid and p* variables? If I can prefix an alphabet to every value, then the code above would create my dummies.
Many thanks!
Vijay
Comment