Straight forward problem that I am just not sure how to structure in the code. I have several patients with labs in a dataset. Each Obs is a a lab collected on a different date. The issue is that I am trying to distinguish if a person in Linked to care (only 1 lab) or if they have been retained in care (more than 1 lab within 1 year of today). I am wanting to create a Linked and retained variable with 1 meaning True and . meaning false. Eventually I will be deleting all observations that are not last.name. How can I use an IF/THEN or IF/THEN DO statement to use historical observations to dictate values on the current line?
Name lab_date result DESIRED>>> Linked Retained
bob 01/01/2016 400 . .
bob 06/15/2016 400 . .
bob 06/01/2017 400 . .
bob 12/31/2017 400 1 1 /*Multiple visits within the 365 days*/
craig 01/01/2015 400 . .
craig 02/09/2018 400 1 . /*Only one visit within 365 days*/
steve 01/01/2015 400 . . /*No visits within timeframe*/
bob 01/21/2018 400 1 . /*Only one visit within 365 days*/
Figure it out!!!! The below code uses the Lag() function which looks back at the previous observation. As long at the patient_ID is the same for the current observation and the previous observation it works for determining if the patient has been receiving care over the course of at least two visits.
The ONLY issue that I have with this is that, I want to be able to look at ALL lad dates for one patient a determine what i need by looking at the observations in the by group together. This ONLY allows me to visit the previous observations. So it works but its not as efficient as I want.
set sample;
by by_group;
if last.by_group and collection_date > '01jan2017'd
then linked=1;
if last.by_group and lag(by_group)=by_group
and collection_date > '01jan2017'd
and collection_date-lag(collection_date)<365
then retained=1;
is this if then question or interval check?
Im not sure what an "Interval check" is and I had fully intended on using If then statements to produce the output I want. That is unless someone can show me a different way to accomplish this. If you can feel free to share, that is why I am here.
Ok no worries, please post a sample of your HAVE(input dataset) and WANT(expected output) and the logic. Thank you
EDITED: I think my assumption is right and understand what you want. Sorry your title confused me
Here is a sample of what I am working with. I created the variables linked and retained. They just need to be filled in as mentioned in the original post. The end goal is to have a set with one observation per patient that would look like this:
(*collection_date viral_load and provider and other variables will most likely be deleted from data sets moving forward)
last ......... collection_date viral_load provider linked retained
bob 01/01/2018 200 other 1 1
bill 05/16/2016 1001 other . .
bret 08/21/2018 78 other 1 .
bacon 01/01/2018 563 other 1 1
Oh my god, are you asking for like?--if yes my bad and sincere apologies
data want;
set have;
by name;
if last.name;
run;
or perhaps i am not understanding this part of what you wrote--"
"How can I use an IF/THEN or IF/THEN DO statement to use historical observations to dictate values on the current line?"
Yes, that is process that I believe that it will take. That is ok. I will continue to play around with it. If/When i figure it out I will post the solution.
Figure it out!!!! The below code uses the Lag() function which looks back at the previous observation. As long at the patient_ID is the same for the current observation and the previous observation it works for determining if the patient has been receiving care over the course of at least two visits.
The ONLY issue that I have with this is that, I want to be able to look at ALL lad dates for one patient a determine what i need by looking at the observations in the by group together. This ONLY allows me to visit the previous observations. So it works but its not as efficient as I want.
set sample;
by by_group;
if last.by_group and collection_date > '01jan2017'd
then linked=1;
if last.by_group and lag(by_group)=by_group
and collection_date > '01jan2017'd
and collection_date-lag(collection_date)<365
then retained=1;
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.