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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.