Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Programming
- /
- How to identify obs where date B is up tp 120 days greater than date A

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 06-08-2021 02:35 AM
(553 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

6 REPLIES 6

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Kawakami,

Thank you for you response. Much appreciated.

Thank you for you response. Much appreciated.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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";
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

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.

**Available on demand!**

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

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.