BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
EM_G
Fluorite | Level 6

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 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

 

View solution in original post

6 REPLIES 6
ballardw
Super User

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

 

EM_G
Fluorite | Level 6
Ballardw,
Thankyou this achieved exactly what I was after. I used the second approach, so other ranges I was not aware of were made visible by the missing values. My dates were already sas dates, so this made it simple. Sorry about the 120 day typo in the title.

Thank you for your response, much appreciated.
japelin
Rhodochrosite | Level 12

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;
EM_G
Fluorite | Level 6
Kawakami,

Thank you for you response. Much appreciated.
Kurt_Bremser
Super User

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";
EM_G
Fluorite | Level 6
KurtBremser,

Thankyou for your response. This also achieves what I was after. There were no B dates smaller than or within 10 days from A, thankfully.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 987 views
  • 0 likes
  • 4 in conversation