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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.