BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
AbletMuhtar
Fluorite | Level 6

Hello All, 

I have a dataset containing dates with gaps, and my objective is to detect these gaps between the dates and subcategorize them based on the continuity of each gap. Thanks.

This is what i have:

Cat1

Cat2

AdmDt

A

1

01/01/2022

A

1

01/02/2022

A

1

01/03/2022

A

1

01/10/2022

A

1

01/11/2022

A

1

01/12/2022

A

2

01/01/2022

A

2

01/02/2022

 

This is what i want:

Cat1

Cat2

AdmDt

Subcategory_id

A

1

01/01/2022

1

A

1

01/02/2022

1

A

1

01/03/2022

1

A

1

01/10/2022

2

A

1

01/11/2022

2

A

1

01/12/2022

2

A

2

01/01/2022

3

A

2

01/02/2022

3

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Use DIF() and check if it's 1 and otherwise, reset the counter. 

This assumes your dates are sorted and in order.

 

data want;
set have;
by cat1 cat2 admdt;

retain subcategory_id;

date_dif =dif(admdt);

if first.cat1 then subcategory_id=1;
else if first.cat2 or date_dif ne 1 then subcategory_id + 1;

*drop date_dif;
run;

View solution in original post

4 REPLIES 4
Reeza
Super User

Use DIF() and check if it's 1 and otherwise, reset the counter. 

This assumes your dates are sorted and in order.

 

data want;
set have;
by cat1 cat2 admdt;

retain subcategory_id;

date_dif =dif(admdt);

if first.cat1 then subcategory_id=1;
else if first.cat2 or date_dif ne 1 then subcategory_id + 1;

*drop date_dif;
run;
AbletMuhtar
Fluorite | Level 6

it worked, thank you 😀

john_mccall
SAS Employee

Hi.  I am not sure if this is what you are looking for, but consider the following:

/* Create sample table*/
Data Example;
infile datalines dlm=',';
Input Cat1:$1. Cat2 :$1. AdmDt :mmddyy10.;
format admdt mmddyy10.;
datalines;
A,1,01/01/2022
A,1,01/02/2022
A,1,01/03/2022
A,1,01/10/2022
A,1,01/11/2022
A,1,01/12/2022
A,2,01/01/2022
A,2,01/02/2022
A,2,01/07/2022
;
run;
/*Sort table*/
proc sort data=example;
by Cat1 Cat2 AdmDt;
/* Use first., last. and lag to check values and assign a value to SubCat*/
Data Categorize;
set example;
by Cat1 Cat2 AdmDt;
Retain SubCat 0;
if first.cat2 then SubCat+1;
If not first.cat2 and admDt >sum(lag(Admdt)+1) then SubCat+1;
run;

AbletMuhtar
Fluorite | Level 6

Thank you, it worked.😀

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 4 replies
  • 185 views
  • 2 likes
  • 3 in conversation