Hi All,
I’m trying to assign visit names based on the
dates. Need help regarding logic. Using SAS 9.3.
i have dataset a, one record per member.
id Date
001 28jan2016
002 04feb2017
dataset b: multiple records per member.need to derive
visit value based on dataset a date value. Date-st is
before the date then visit values should be visit-1,visit-2
Etc...if Date-st is after the Date then visit values should be
visit+1 , visit+2 etc. Different number of visits per subject
. If Date-st is missing then need to use Date-ed to
assign visit.
id Date-st Date-ed Visit
001 27jan2016 29jan2017 Visit-1
001 28jan2016 29jan2017 Visit0
001 29jan2016 30jan2017 Visit+1
002 4feb2017 6feb2017 Visit0
002. 6feb2017 7feb2017 Visit1
002 8feb2017 9feb2017 Visit2
003 . 10feb2017 Visit3
As I just started on SAS, Need help me with the coding.thanks for your help.
Thanks in advane.
Is there any real reason to include "visit" in the variable if the text is in every single value?
Also please confirm that your dates as SAS date valued numeric variables with a SAS date9. format applied.
Note that you have made things somewhat difficult by not giving us the names of the datasets and using variable names that by default are not valid for SAS variables (date-st date-ed, hyphens aren't normally valid SAS variables).
Also for id 002 you compare 04feb2017 with
Is there any real reason to include "visit" in the variable if the text is in every single value?
Also please confirm that your dates as SAS date valued numeric variables with a SAS date9. format applied.
Note that you have made things somewhat difficult by not giving us the names of the datasets and using variable names that by default are not valid for SAS variables (date-st date-ed, hyphens aren't normally valid SAS variables).
Also for id 002 you compare 04feb2017 with
It can be done like this:
proc sort data=b; by id date_st date_ed; run;
data want;
length visit $10;
nn = 0;
do until (last.id);
merge a b; by id;
nn = nn - (coalesce(date_st, date_ed) < date);
end;
do until (last.id);
merge a b; by id;
if coalesce(date_st, date_ed) = date then nn = 0;
visit = cats("Visit", nn);
output;
nn = nn + 1;
if nn = 0 then nn = 1; /* Skip visit0 if date is not found */
end;
drop nn;
run;
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.