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

I have below data and trying to add a column 'count if <7 days'

I want to count if specific ID is reoccurring within 7 days of the last one. 

Thanks in advance 🙂

 

ID Date Count if < 7 days
123 1/04/2021 0
123 5/04/2021 1
123 9/04/2021 1
123 30/04/2021 0
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Here one way how to go about this.

data have;
  infile datalines truncover;
  input id date:ddmmyy10. result_expected;
  format date date9.;
  datalines;
123 1/04/2021 0
123 5/04/2021 1
123 9/04/2021 1
123 30/04/2021 0
;

data want;
  set have;
  by id date;
  result_calculated= date-lag(date)<7 and not first.id;
run;

 The logical expression "date-lag(date)<7 and not first.id" will return 1 if TRUE and 0 if FALSE.

View solution in original post

4 REPLIES 4
Patrick
Opal | Level 21

Here one way how to go about this.

data have;
  infile datalines truncover;
  input id date:ddmmyy10. result_expected;
  format date date9.;
  datalines;
123 1/04/2021 0
123 5/04/2021 1
123 9/04/2021 1
123 30/04/2021 0
;

data want;
  set have;
  by id date;
  result_calculated= date-lag(date)<7 and not first.id;
run;

 The logical expression "date-lag(date)<7 and not first.id" will return 1 if TRUE and 0 if FALSE.

ywon111
Quartz | Level 8
Thanks for the solution.
Is it possible to get working days <7 ?
Patrick
Opal | Level 21

@ywon111 wrote:
Thanks for the solution.
Is it possible to get working days <7 ?

You don't have actual dates so you wouldn't know on which day a user started (weekday, week-end, holiday). Given that you're looking for <7 working days which will always include a week-end I guess best you could do is change the expression to: "date-lag(date)<9 and not first.id"

 

ywon111
Quartz | Level 8
Is there a way to add into the codes to exclude certain dates?

E.g. don't count if date = 1/04/21

datalines;
123 1/04/2021 0
123 5/04/2021 0
123 9/04/2021 1
123 30/04/2021 0
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
  • 4 replies
  • 1537 views
  • 0 likes
  • 2 in conversation