BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kingsii24
Fluorite | Level 6

I have the following data set:

Lag_Data_Have_031120.PNG

 

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:

Lag_Data_Want_031120.PNG

 

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).

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

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,

View solution in original post

3 REPLIES 3
ed_sas_member
Meteorite | Level 14

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,

kingsii24
Fluorite | Level 6

@ed_sas_member 

 Thank you so much! This is what I needed, the discrepancy between the two data sets is definitely an error on my side.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1144 views
  • 2 likes
  • 2 in conversation