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

I would like to calculate the date differences between activities for each ID, then tag those dates that happened within 30 days or less.  All advice is greatly appreciated. My first step would be to sort based on id and activity date. 

 

Here is an example of my data: 

ID

Activity Date

1234

20200110

1234

20200120

1234

20200601

5678

20200110

5678

20200129

5678

20200220

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

First, you need to treat Activity_Date as a numeric variable in SAS, and convert it to a true SAS date value (which is the number of days since 01JAN1960). This done in my INPUT statement using the informat YYMMDD8. Then the difference between dates is just a subtraction.

 

data have;
input ID Activity_Date yymmdd8.;
cards;
1234 20200110
1234 20200120
1234 20200601
5678 20200110
5678 20200129
5678 20200220
;
data want;
    set have;
    by id;
    delta=activity_date-lag(activity_date);
    if first.id then delta=.;
run;
--
Paige Miller

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

First, you need to treat Activity_Date as a numeric variable in SAS, and convert it to a true SAS date value (which is the number of days since 01JAN1960). This done in my INPUT statement using the informat YYMMDD8. Then the difference between dates is just a subtraction.

 

data have;
input ID Activity_Date yymmdd8.;
cards;
1234 20200110
1234 20200120
1234 20200601
5678 20200110
5678 20200129
5678 20200220
;
data want;
    set have;
    by id;
    delta=activity_date-lag(activity_date);
    if first.id then delta=.;
run;
--
Paige Miller
a_zacMD
Obsidian | Level 7
thank you, this works great!!!
ballardw
Super User

Is that value an actual SAS date? Meaning a numeric value with a SAS date format applied such as YYMMDDN8. ?

If not the first thing will be to create an actual date value so that the comparisons are easy.

a_zacMD
Obsidian | Level 7
No, the date value is 20200801 = YYYYMMDD.

PaigeMiller
Diamond | Level 26

@a_zacMD wrote:
No, the date value is 20200801 = YYYYMMDD.


So it has to be converted to a valid SAS date, which I showed how to do, and then the calculations are trivial.

--
Paige Miller
ballardw
Super User

@a_zacMD wrote:
No, the date value is 20200801 = YYYYMMDD.


Actually no it isn't. If it were a real date then adding 1 to the data would make 20201231 20200101. If you get 20201232 that is why it is not a date. SAS dates are numeric but use Formats to display the number as the desired date. That also allows all the SAS date related functions to work.

 

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

a_zacMD
Obsidian | Level 7
Thank you for posting this!!

sas-innovate-2024.png

Available on demand!

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

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 551 views
  • 1 like
  • 3 in conversation