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

What im trying to do is determine the days between dates for a particular customer.

This is ideally what i would like the output to look like.

Customer_id     contact_date     Days

123                    01-01-2014       0

123                    04-01-2014       3    

123                    09-01-2014       5

123                    11-01-2014       2

124                    01-01-2014       0

124                    11-01-2014     10 

I have tried sorting by customer_id and then performing a first.customer_id and then retaining the contact_date however it doesnt seem to work.

Data days;

set contact_date;

by customer_id;

retain first_day;

if first.customer_id then do

first_day = contact_date;

end;

else

  do;

  if nmiss(contact_date,first_day)= 0 then days_diff = contact_date - first_day;

  else first_day = targetted_date;

  end;

run;

This results in the difference being from the first_date which i dont want, so its not quite what i want, i want the subtraction to be from the previous record not the first record.

Any assistance would be greatly appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
Scott_Mitchell
Quartz | Level 8

How about this?

DATA WANT;

  SET HAVE;

  BY CUSTOMER_ID;

  FORMAT PREV_DAY DATE9.;

  PREV_DAY = LAG(CONTACT_DATE);

  IF FIRST.CUSTOMER_ID THEN DAYS = 0;

  ELSE DAYS = DATDIF(PREV_DAY,CONTACT_DATE,"ACT/ACT");

RUN;

We use the lag function to retain the value from the previous observation.  You can drop the PREV_DATE variable.  I just left it in so you can see what is happening.

View solution in original post

2 REPLIES 2
Scott_Mitchell
Quartz | Level 8

How about this?

DATA WANT;

  SET HAVE;

  BY CUSTOMER_ID;

  FORMAT PREV_DAY DATE9.;

  PREV_DAY = LAG(CONTACT_DATE);

  IF FIRST.CUSTOMER_ID THEN DAYS = 0;

  ELSE DAYS = DATDIF(PREV_DAY,CONTACT_DATE,"ACT/ACT");

RUN;

We use the lag function to retain the value from the previous observation.  You can drop the PREV_DATE variable.  I just left it in so you can see what is happening.

rjnc13
Calcite | Level 5

Thanks a lot for the prompt response, it works correctly, much appreciated!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 1718 views
  • 0 likes
  • 2 in conversation