I have a labour force survey dataset which I am working with. In this dataset, each worker has an occupation group and an industry. I want to create a variable that combines these two variables and I have managed to do so using a loop. (I had to convert each of the variables to string variables beforehand).
Code:
tostring occupation, gen (stroccupation) gen strindustry = string(industry, "%02.0f") foreach x in stroccupation { gen occupationxindustry = `x' + strindustry }
For instance, for occupationxindustry==1109 I need to assign the label "CE_seniorofficials_legislatorsxCommunity" and for occupationxindustry==1107 I need "CE_seniorofficials_legislatorsxTransport".
I could do this manually, but I have 27 occupation groups and 10 industries, so it would be a very tedious task. I have tried a number of different iterations in a loop, but I am still new to these. Any suggestions would be welcomed; I include the variables below.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float(occupation industry) str2(stroccupation strindustry) str4 occupationxindustry 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 7 "11" "07" "1107" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 11 9 "11" "09" "1109" 12 9 "12" "09" "1209" 12 6 "12" "06" "1206" 12 7 "12" "07" "1207" 12 3 "12" "03" "1203" 12 7 "12" "07" "1207" 12 3 "12" "03" "1203" 12 7 "12" "07" "1207" 12 2 "12" "02" "1202" 12 4 "12" "04" "1204" 12 8 "12" "08" "1208" 12 7 "12" "07" "1207" 12 2 "12" "02" "1202" 12 2 "12" "02" "1202" 12 8 "12" "08" "1208" 12 9 "12" "09" "1209" 12 8 "12" "08" "1208" 12 2 "12" "02" "1202" 12 6 "12" "06" "1206" 12 6 "12" "06" "1206" 12 2 "12" "02" "1202" 12 8 "12" "08" "1208" 12 5 "12" "05" "1205" 12 6 "12" "06" "1206" 12 3 "12" "03" "1203" 12 8 "12" "08" "1208" 12 6 "12" "06" "1206" 12 6 "12" "06" "1206" 12 6 "12" "06" "1206" 12 6 "12" "06" "1206" 12 6 "12" "06" "1206" 12 . "12" "." "12." 12 9 "12" "09" "1209" 12 8 "12" "08" "1208" 12 9 "12" "09" "1209" 12 8 "12" "08" "1208" 12 9 "12" "09" "1209" 12 6 "12" "06" "1206" 12 3 "12" "03" "1203" 12 8 "12" "08" "1208" 12 3 "12" "03" "1203" 12 3 "12" "03" "1203" 12 8 "12" "08" "1208" 12 7 "12" "07" "1207" 12 8 "12" "08" "1208" 12 3 "12" "03" "1203" 12 8 "12" "08" "1208" 12 6 "12" "06" "1206" 12 9 "12" "09" "1209" 12 7 "12" "07" "1207" 12 3 "12" "03" "1203" 12 2 "12" "02" "1202" 12 8 "12" "08" "1208" 12 8 "12" "08" "1208" 12 8 "12" "08" "1208" 12 3 "12" "03" "1203" 12 9 "12" "09" "1209" 12 7 "12" "07" "1207" 12 2 "12" "02" "1202" 12 9 "12" "09" "1209" 12 7 "12" "07" "1207" 12 6 "12" "06" "1206" 12 6 "12" "06" "1206" 12 9 "12" "09" "1209" 12 4 "12" "04" "1204" 12 3 "12" "03" "1203" 12 8 "12" "08" "1208" 12 9 "12" "09" "1209" 12 5 "12" "05" "1205" 12 2 "12" "02" "1202" 12 9 "12" "09" "1209" 12 9 "12" "09" "1209" 12 7 "12" "07" "1207" 12 3 "12" "03" "1203" end label values occupation occupation label def occupation 11 "CE_seniorofficials_legislators", modify label def occupation 12 "Admin_commercial_managers", modify label values industry industry label def industry 2 "Mining", modify label def industry 3 "Manufacturing", modify label def industry 4 "Utilities", modify label def industry 5 "Construction", modify label def industry 6 "Trade", modify label def industry 7 "Transport", modify label def industry 8 "Financial", modify label def industry 9 "Community", modify
0 Response to How to combine variable labels into a single variable
Post a Comment