Hi All,
I have patient Id's with Date A and Date B. I am trying to assign 'Yes' to the patients where Date B is 11 to 20 days greater than Date A an 'No' to any Id where Date B is 21 or more days greater than Date A.
ID DateA DateB Y/N
1 10May17 25May17 Y
3 8Nov18 10Dec18 N
2 13June17 26June17 Y
Where do I start?
Thanks in advance
You start with SAS date valued variables. If your variables are 1) numeric and 2) have format like DATE7. assigned your likely good. If the format is $ anything the first step will be to get the SAS dates using INPUT.
SAS dates are number of days since 1Jan1960. So when you are interested in days you can do simple subraction:
date want; set have; NewVar = (11 le (dateb -datea) le 20); run;
Will create a numeric variable valued as 1 when the difference is in the range and 0 otherwise. SAS uses 1 as true and 0 as false and is much more flexible than assigning character Y N values. If you must "see" Y or N in a report it takes about 3 minutes or less to write a custom format to display the numeric 1 as 'Y', 'Yes', 'True' or "You bet your bippy" and any other text for the 0.
Your question title says 120 days, so which is it, 20 or 120?
When you specify something like this you really should explicitly state what to assign when other ranges result.
So perhaps another approach:
date want; set have; if (11 le (dateb -datea) le 20 ) then Newvar=1; else if (dateb -datea) > 20 then NewVar=0; run;
which leaves Newvar with missing values
You start with SAS date valued variables. If your variables are 1) numeric and 2) have format like DATE7. assigned your likely good. If the format is $ anything the first step will be to get the SAS dates using INPUT.
SAS dates are number of days since 1Jan1960. So when you are interested in days you can do simple subraction:
date want; set have; NewVar = (11 le (dateb -datea) le 20); run;
Will create a numeric variable valued as 1 when the difference is in the range and 0 otherwise. SAS uses 1 as true and 0 as false and is much more flexible than assigning character Y N values. If you must "see" Y or N in a report it takes about 3 minutes or less to write a custom format to display the numeric 1 as 'Y', 'Yes', 'True' or "You bet your bippy" and any other text for the 0.
Your question title says 120 days, so which is it, 20 or 120?
When you specify something like this you really should explicitly state what to assign when other ranges result.
So perhaps another approach:
date want; set have; if (11 le (dateb -datea) le 20 ) then Newvar=1; else if (dateb -datea) > 20 then NewVar=0; run;
which leaves Newvar with missing values
how about this code?
data have;
length id dateA dateB 8 YN $1;
input id dateA:date9. dateB:date9.;
format dateA dateB date9.;
datalines;
1 10May17 25May17
3 8Nov18 10Dec18
2 13Jun17 26Jun17
;
run;
data want;
set have;
if 11=<dateB-dateA=<20 then YN='Y';
else if 21=<dateB-dateA then YN='N';
run;
What about B dates smaller than or within 10 days from A?
Anyway, SAS dates are counts of days, so you can do
if 11 le dateb - datea le 20
then y_n = "Yes";
else if dateb - datea gt 20 then y_n = "No";
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.