I have a cross-sectional survey with 12 sets of variables, e.g. job1_*, job2_*, ..., job12_*, that cover the entire employment history starting with job1 being the first/oldest job and if people have had multiple jobs job2 to job12 recoding the most recent job. Variables job*_01 record the type of employment, e.g. temporary, permanent, seasonal, while job*_02 covers the occupation. I would now like to create new variables jobmin1_*, jobmin2_*, jobmin3_* that reverse the order of the employment history, so that the variables jobmin1_* is the most recent job and jobmin12* is the first/oldest job. The problem is that individuals have different number of jobs so I need to dynamically select the oldest job.
MRE Starting point:
MRE Expected end result:
My solution so far is to create a variable that captures the number of jobs people had, and then create the variables for each suffix and replace the variables with the value of the respective job variable by using the information from the n_jobs variable. However, in my solution jobmin2_01 is wrong for observation 6 (3 instead of 1) and observation 10 (3 instead of 1). My best guess is that the evaluation of `=jobcount-1' in the last two replace lines does not work properly and I have been playing around with different forms, e.g. making jobcount-2 in the last replace line, but I these just throw error messages and I can not get them to work.
My attempt:
MRE Starting point:
Code:
* Example generated by -dataex-. For more info, type help dataex clear input byte(job1_01 job2_01 job3_01) long(job1_02_nh job2_02_nh job3_02_nh) 4 1 . 9211 5212 . 1 1 . 9510 5419 . 1 1 . 5329 5329 . 1 1 1 7112 3142 5221 1 3 . 2269 6130 . 3 1 1 9520 8142 5211 1 1 . 5212 2341 . 1 1 1 9629 9510 5221 1 1 . 5230 5246 . 3 1 1 9212 9212 9211 end
Code:
clear input byte(job1_01 job2_01 job3_01) long(job1_02_nh job2_02_nh job3_02_nh) byte(jobmin1_01 jobmin2_01 jobmin3_01) long(jobmin1_02_nh jobmin2_02_nh jobmin3_02_nh) 4 1 . 9211 5212 . 1 4 . 5212 9211 . 1 1 . 9510 5419 . 1 1 . 5419 9510 . 1 1 . 5329 5329 . 1 1 . 5329 5329 . 1 1 1 7112 3142 5221 1 1 1 5221 3142 7112 1 3 . 2269 6130 . 3 1 . 6130 2269 . 3 1 1 9520 8142 5211 1 1 3 5211 8142 9520 1 1 . 5212 2341 . 1 1 . 2341 5212 . 1 1 1 9629 9510 5221 1 1 1 5221 9510 9629 1 1 . 5230 5246 . 1 1 . 5246 5230 . 3 1 1 9212 9212 9211 1 1 3 9211 9212 9212 end
My attempt:
Code:
* Step 1: Capture the number of jobs people have had * Create the number of jobs an individual has had gen n_jobs = 0 forval i = 1/3 { replace n_jobs = n_jobs + !missing(job`i'_01) } clonevar jobcount = n_jobs // Step 2: Loop Over Suffixes and Assign Values for Up to Three Jobs local suffixes = "01 02_nh" foreach suffix in `suffixes' { // Assign jobmin1_* with the most recent job gen jobmin1_`suffix' = . gen jobmin2_`suffix' = . gen jobmin3_`suffix' = . replace jobmin1_`suffix' = job`=jobcount'_`suffix' replace jobmin2_`suffix' = job`=jobcount-1'_`suffix' if n_jobs >= 2 replace jobmin3_`suffix' = job`=jobcount-1'_`suffix' if n_jobs >= 3 } list job1_01 job2_01 job3_01 jobmin1_01 jobmin2_01 jobmin3_01 n_jobs
Comment