Hello
I want to calculate for each customer the difference between wealth in first time and last time.
I want to create 2 wanted data set.
In "want1" data set the difference between last and first will be dispalyed in last row.
In "Want2" data set only dispaly the last row with the information of wealth_first,wealth_last,difference
what is the way to do it please?
Data have;
Input Cust_ID time $ wealth;
cards;
1 2022-01 100
1 2022-02 90
1 2022-03 85
1 2022-04 130
2 2022-01 400
2 2022-02 410
2 2022-03 380
2 2022-04 270
2 2022-05 150
2 2022-06 40
;
Run;
Data want1;
Input Cust_ID time $ wealth dif;
1 2022-01 100 .
1 2022-02 90 .
1 2022-03 85 .
1 2022-04 130 30
2 2022-01 400 .
2 2022-02 410 .
2 2022-03 380 .
2 2022-04 270 .
2 2022-05 150
2 2022-06 40 -360
;
Run;
Data want1;
Input Cust_ID time $ wealth_first wealth_last dif;
1 2022-04 100 130 30
2 2022-06 400 40 -360
;
Run;
Can be done in one step:
data want;
set have;
by cust_id;
retain wealth_first;
if first.cust_id then wealth_first = wealth;
if last.cust_id;
wealth_last = wealth;
dif = wealth_last - wealth_first;
drop wealth;
run;
Can be done in one step:
data want;
set have;
by cust_id;
retain wealth_first;
if first.cust_id then wealth_first = wealth;
if last.cust_id;
wealth_last = wealth;
dif = wealth_last - wealth_first;
drop wealth;
run;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.