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

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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