BookmarkSubscribeRSS Feed
nbora
Calcite | Level 5

I have a ( kind of specific) problem, I would say.
Here is the dataset I have:

data have;
    infile datalines delimiter='|' dsd truncover;
    input client $ account $ start :date9. end :date9.;
    format start end date9.;
datalines;
123 | 123ABC | 30JUN2019 | 30SEP2019
321 | 123ABC | 30JUN2020 | 31JUL2020
456 | 456ABC | 30JUN2021 | 30SEP2023
456 | 456ABC | 30JUN2018 | 30NOV2019
654 | 456ABC | 01DEC2019 | 05FEB2020
;
run;

 

For this data I want to compare the end date from the current datapoint with the start date of the next datapoint and calculate the date difference between these two dates. If the difference is shorter than 4 days I want to merge the two data points. So in this case the output should be:

data have;
    infile datalines delimiter='|' dsd truncover;
    input client $ account $ start :date9. end :date9.;
    format start end date9.;
datalines;
123 | 123ABC | 30JUN2019 | 30SEP2019
321 | 123ABC | 30JUN2020 | 31JUL2020
456 | 456ABC | 30JUN2021 | 30SEP2023
456 | 456ABC | 30JUN2018 | 05FEB2020
;
run;

ChatGPT did fail to produce the desired output.

Cheers,

B

3 REPLIES 3
ballardw
Super User

You really want to combine dates across different clients????

nbora
Calcite | Level 5

Yes. Even if they different clients the "belong" to the same account.

JOL
SAS Employee JOL
SAS Employee

The following code works based on the data provided. This assumes the data is sorted by client and uses by group processing and the lag function.

 

data have2;
set have;
by client;
p_end = lag(end);
p_start = lag(start);
p_client = lag(client);
p_account = lag(account);
diff = abs(p_end-start);
format p_start p_end date9.;

if 0<= diff <=4 then
do;
client = p_client;
account = p_account;
start = p_start;
end;

if first.client =0 then delete;
keep client account start end;
run;

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