I am currently working on a dataset which contains records of daily time use for a large number of participants (American Time Use Survey activity data). I am trying to modify some data before proceeding with the next steps, but my SAS code is not working as intended. Here’s the condition I am trying to implement in the code: If a participant (same TUCASEID ) has only two commute periods ( TRCODE in 180501, 180503, 180504), where: One period occurs right before work ( TUTIER2CODE=05 ) and the other right after work. he two periods differ by less than 15 minutes in duration ( TUACTDUR24 ). One record of commute modality is valid ( TEWHERE between 12 and 21), but the other is invalid ( TEWHERE less than 12). In this case, I want to replace the invalid modality with the valid modality. I tried the code below, but the results are different from what I expected. Which parts need to be changed? data replaced_2023; set baseline_2023; by TUCASEID; if TRCODE in (180501, 180503, 180504) then do; if 12 <= TEWHERE <= 21 then valid_modality = 1; else if TEWHERE < 12 then valid_modality = 0; end; retain prev_TUTIER1CODE prev_TUACTDUR24 prev_TEWHERE prev_valid_modality; if first.TUCASEID then do; prev_TUTIER1CODE = .; prev_TUACTDUR24 = .; prev_TEWHERE = .; prev_valid_modality = .; end; if TUTIER1CODE = 05 and prev_TUTIER1CODE = 05 and abs(TUACTDUR24 - prev_TUACTDUR24) < 15 and prev_valid_modality = 1 and valid_modality = 0 then TEWHERE = lag(prev_TEWHERE); run; data last_replaced_2023; set replaced_2023; by TUCASEID; if TRCODE in (180501, 180503, 180504) then do; if 12 <= TEWHERE <= 21 then valid_modality = 1; else if TEWHERE < 12 then valid_modality = 0; end; retain next_TUTIER2 next_TUACTDUR24 next_TEWHERE next_valid_modality; if first.TUCASEID then do; next_TUTIER1CODE = .; next_TUACTDUR24 = .; next_TEWHERE = .; next_valid_modality = .; end; if TUTIER1CODE = 05 and prev_TUTIER1 = 05 and abs(TUACTDUR24 - prev_TUACTDUR24) < 15 and next_valid_modality = 1 and next_modality = 0 then TEWHERE = lag(next_TEWHERE); run;
... View more