BookmarkSubscribeRSS Feed
Jerram
Calcite | Level 5

Hi,

I have a data set such as the following, with date format YYMMDD10:

 

ID         Date            Count -5 days
004 01/11/2016 0 007 2011/03/10 0 007 2012/12/04 0 007 2012/12/05 1 007 2012/12/05 2 007 2012/12/07 3 008 2012/10/01 0 008 2012/10/04 1 010 2014/11/25 0 010 2015/04/19 0 010 2015/04/20 1

 

I want to count how many times the ID variable occurs within -5 days, or potentially +/-5 days.

 

Any help would be really appreciated.

 

Thanks

2 REPLIES 2
Kurt_Bremser
Super User

A basic SQL solution looks like this:

data have;
input id date :yymmdd10.;
format date yymmdd10.;
cards;
004        2016/01/11
007        2011/03/10
007        2012/12/04
007        2012/12/05
007        2012/12/05
007        2012/12/07
008        2012/10/01
008        2012/10/04
010        2014/11/25
010        2015/04/19
010        2015/04/20
;
run;

proc sql;
create table want as
select
  a.id,
  a.date,
  (select count(*) from have b where a.id = b.id and 0 <= a.date - b.date <= 5) -1 as count
from have a
;
quit;

but it delivers a different count for the first of the double dates.

Note how I presented your example data in a data step, so that others can easily recreate the dataset.

ballardw
Super User

If your requirement is to NOT count the same date you need to explicitly include that in your requirement. Otherwise the same date will count as it is within 5 (or 7 or whatever ) since 0 is in pretty much all -i to +i intervals.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 2630 views
  • 0 likes
  • 3 in conversation