BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ronein
Onyx | Level 15

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 1081 views
  • 2 likes
  • 2 in conversation