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

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—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
  • 4 replies
  • 1549 views
  • 0 likes
  • 2 in conversation