Sorry if the title isn't quite accurate, but it's the most succinct way I could think. I have a large dataset in which some individuals have multiple tests (each individual is given a unique ID). Some of my team members are interested in identifying time to follow up on elevated tests and I'm not quite sure how to do that in SAS. Functionally I need to create a variable that indicates whether the previous test for that ID was elevated in cases where an ID has multiple tests on different dates. Any help with this would be greatly appreciated.
Please show some sample data, in the form of a working data step.
And the corresponding desired output.
Help us help you.
I do not have access to SAS right now to test the data step, I'll provide what some truncated data looks like and what I'd like the end product to be.
data test;
input ID TestDate Elevated $;
datalines;
1 20200101 Y
1 20200105 N
1 20200110 N
2 20200106 Y
2 20200108 N
3 20200112 Y
3 20200114 Y
3 20200125 Y;
run;
ID TestDate Elevated
1 20200101 Y
1 20200105 N
1 20200110 N
2 20200106 Y
2 20200108 N
3 20200112 Y
3 20200114 Y
3 20200125 Y
What I'd like the data to look like with the new variable is this
ID TestDate Elevated Prior Elevated
1 20200101 Y NA
1 20200105 N Y
1 20200110 N N
2 20200106 Y NA
2 20200108 N Y
3 20200112 Y NA
3 20200114 Y Y
3 20200125 Y Y
Something like
data want;
set test;
by Id;
prior_elevated = lag(Elevated);
if first.id then prior_elevated = ' ';
run;
should do it.
Thanks for posting sample data.
The response by @andreas_lds will certainly produce your sample output.
But how does your sample output provide "time to follow up on elevated tests" which you described as your teammates' interest in your original post?
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!
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.