Below is my data:
Code:
dataex ID emplperjob1 job1
"111" 5 "Accountant"
For brevity, I have only included employees per job (emplperjob) and job 1, there are 25 jobs (thus 25 job variables: job1, job2, etc.) and 25 emplperjob variables (emplperjob1, emplperjob2, etc).
Suppose accountant is job 1. There are unfortunately instances in this dataset in which accountants are also listed under e.g. job2 (I chose job2 here for this example, but it could be any of the other 24 jobs).
For instance, considering the below table:
ID | emplperjob1 | emplperjob2 | job1 | job2 |
111 | 5 | 6 | accountant | accountant |
I would like to create a variable agg_emplperjob1 that sums emplperjob1 and emplperjob2 if the values of job1 and job2 (strings) are equal. Here, agg_emplperjob1= 5+6=11. If the values are not equal, Stata should just leave the emplperjob variables as is.
I know the below code is erroneous, but it was my attempt:
Code:
foreach i=1/25{ foreach j=1/25{ by ID, sort: egen agg_emplperjob`i'=cond("job"`j'="job"`j',rowtotal(emplperjob`i'),emplperjob`i') } }
Many thanks in advance!
Best regards,
Maxence Morlet
0 Response to Rowtotal of Multiple Variables Conditional on Other Variables being equal
Post a Comment