I have the following data set:
I'm trying to figure out how to capture the previous (most recent) training of an employee (represented by ID) given that they took the IT training. My goal is to have the following table:
I'm assuming that I can use the lag function to accomplish this but I'm having a hard time trying to figure out how to set up my SAS code (using SAS studio).
Hi @kingsii24
Welcome to the community!
You can try this:
proc sort data=have out=have_sorted;
by ID Date_start;
run;
data want;
set have_sorted;
by ID Date_start;
length prev_training $ 10;
_lag = lag(training_type);
if first.ID then _lag="";
if training_type = "IT" then prev_training = _lag;
drop _lag;
run;
NB: there are some inconsistencies in the pictures for patient 2 -> the start dates for each training are not the same. I believe it is a mistake.
If not, please explain.
Best,
Hi @kingsii24
Welcome to the community!
You can try this:
proc sort data=have out=have_sorted;
by ID Date_start;
run;
data want;
set have_sorted;
by ID Date_start;
length prev_training $ 10;
_lag = lag(training_type);
if first.ID then _lag="";
if training_type = "IT" then prev_training = _lag;
drop _lag;
run;
NB: there are some inconsistencies in the pictures for patient 2 -> the start dates for each training are not the same. I believe it is a mistake.
If not, please explain.
Best,
Thank you so much! This is what I needed, the discrepancy between the two data sets is definitely an error on my side.
Thank you @kingsii24!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.