Hello
I want to calculate the difference between two dates by row per customer_id.
Please find the raw data
data a;
input CUSTOMER_ID date anydtdte23.;
format date date9.;
cards;
1057086 21AUG2014
1057086 25AUG2014
1057086 17SEP2014
1057086 17SEP2014
1057086 19SEP2014
1057086 26SEP2014
9888221 01Feb2014
9888221 23Apr2014
9888221 23Apr2014
9888221 26May2014
9888221 27May2014
;
Run;
Use the dif() function, and set the result to missing if first.customer_id.
Thank you.
For second customer 9888221 in first row I get -237 instead of null.
Why?
data a;
input CUSTOMER_ID date anydtdte23.;
format date date9.;
cards;
1057086 21AUG2014
1057086 25AUG2014
1057086 17SEP2014
1057086 17SEP2014
1057086 19SEP2014
1057086 26SEP2014
9888221 01Feb2014
9888221 23Apr2014
9888221 23Apr2014
9888221 26May2014
9888221 27May2014
;
Run;
proc sort data=a; by customer_id date; run;
Data b;
set a;
days_between = dif(date);
IF first.customer_id then days_between=.;
Run;
Data b;
set a;
by customer_id ;
days_between = dif(date);
IF first.customer_id then days_between=.;
Run;
first. and last. variables are only set if a proper by statement is present. Using a first. variable without a by causes a related message in the log.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.