Hello,
I’m using Stata BE/17.0 and I want to sum the numbers of distinct diplomas students have. The maximum of diploma a student can have is 3 and each diploma is indicated under the 3 string variables diplo1 diplo2 diplo3. There are 3 possible diplomas (des dep cfms) and I want to create 3 new numeric variables (sum_des sum_dep sum_cfms) that would range from 0 to 3. The "sd" value is “without diploma”, so it would count as a zero.
I tried a loop and failed miserably. I finally did it more "manually", but I presume there is a better code that would count the number of distinct diplomas each student has. Here what I did anyways:
Here is the sample data:
Thank you. Any input is appreciated.
N
I’m using Stata BE/17.0 and I want to sum the numbers of distinct diplomas students have. The maximum of diploma a student can have is 3 and each diploma is indicated under the 3 string variables diplo1 diplo2 diplo3. There are 3 possible diplomas (des dep cfms) and I want to create 3 new numeric variables (sum_des sum_dep sum_cfms) that would range from 0 to 3. The "sd" value is “without diploma”, so it would count as a zero.
I tried a loop and failed miserably. I finally did it more "manually", but I presume there is a better code that would count the number of distinct diplomas each student has. Here what I did anyways:
Code:
foreach dip in des dep cfms { forvalues num= 1/3{ gen `dip'`num' = 0 } } forvalue num = 1/3{ replace des`num'=1 if diplo`num'=="des" } gen sum_des= des1+des2+des3 drop des1 des2 des3 forvalue num = 1/3{ replace dep`num'=1 if diplo`num'=="dep" } gen sum_dep= dep1+dep2+dep3 drop dep1 dep2 dep3 forvalue num = 1/3{ replace cfms`num'=1 if diplo`num'=="cfms" } gen sum_cfms= cmfs1+cmfs2+cmfs3 drop cmfs1 cmfs2 cmfs3
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str34 diplo1 str4(diplo2 diplo3) "cfms" " cfms " "" "sd" "" "" "dep" "dep" "dep" "des" "" "" "sd" "" "" "dep" "dep" "" "cfms" "" "" "des" "" "" "des" "dep" "" "des" "" "" "sd" "" "" end
Thank you. Any input is appreciated.
N
Comment