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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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