BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sasuser_sk
Quartz | Level 8

Hi Everyone: Help will be greatly appreciated. Thanks!

 

I have this dataset:

ID MONTH AMOUNT
1679 201611 16.47
1679 201612 1.95
1679 201701 -1.95
1679 201702 188.56
4656 201611 287.52
4656 201612 277.92
4656 201701 285
4656 201702 277.87
4656 201703 284.18
4656 201704 282.37

 

I want to create Total_Amount Column that adds Amount based on ID and want output to look like this:

ID MONTH AMOUNT TOTAL_AMOUNT
1679 201611 16.47 16.47
1679 201612 1.95 18.42
1679 201701 -1.95 16.47
1679 201702 188.56 205.03
4656 201611 287.52 287.52
4656 201612 277.92 565.44
4656 201701 285 850.44
4656 201702 277.87 1128.31
4656 201703 284.18 1412.49
4656 201704 282.37 1694.86

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

That's a RUNNING TOTAL.

  • Retain - holds value across rows
  • BY group processing to ensure that the process is reset at the first of each ID.
  • SUM() function in case of missing data 

 

data want;
set have;
by ID;

retain RUNNING_TOTAL;

if first.ID then RUNNING_TOTAL = AMOUNT;
else RUNNING_TOTAL = sum(RUNNING_TOTAL, AMOUNT);

run;

@sasuser_sk wrote:

Hi Everyone: Help will be greatly appreciated. Thanks!

 

I have this dataset:

ID MONTH AMOUNT
1679 201611 16.47
1679 201612 1.95
1679 201701 -1.95
1679 201702 188.56
4656 201611 287.52
4656 201612 277.92
4656 201701 285
4656 201702 277.87
4656 201703 284.18
4656 201704 282.37

 

I want to create Total_Amount Column that adds Amount based on ID and want output to look like this:

ID MONTH AMOUNT TOTAL_AMOUNT
1679 201611 16.47 16.47
1679 201612 1.95 18.42
1679 201701 -1.95 16.47
1679 201702 188.56 205.03
4656 201611 287.52 287.52
4656 201612 277.92 565.44
4656 201701 285 850.44
4656 201702 277.87 1128.31
4656 201703 284.18 1412.49
4656 201704 282.37 1694.86


 

View solution in original post

4 REPLIES 4
Reeza
Super User

That's a RUNNING TOTAL.

  • Retain - holds value across rows
  • BY group processing to ensure that the process is reset at the first of each ID.
  • SUM() function in case of missing data 

 

data want;
set have;
by ID;

retain RUNNING_TOTAL;

if first.ID then RUNNING_TOTAL = AMOUNT;
else RUNNING_TOTAL = sum(RUNNING_TOTAL, AMOUNT);

run;

@sasuser_sk wrote:

Hi Everyone: Help will be greatly appreciated. Thanks!

 

I have this dataset:

ID MONTH AMOUNT
1679 201611 16.47
1679 201612 1.95
1679 201701 -1.95
1679 201702 188.56
4656 201611 287.52
4656 201612 277.92
4656 201701 285
4656 201702 277.87
4656 201703 284.18
4656 201704 282.37

 

I want to create Total_Amount Column that adds Amount based on ID and want output to look like this:

ID MONTH AMOUNT TOTAL_AMOUNT
1679 201611 16.47 16.47
1679 201612 1.95 18.42
1679 201701 -1.95 16.47
1679 201702 188.56 205.03
4656 201611 287.52 287.52
4656 201612 277.92 565.44
4656 201701 285 850.44
4656 201702 277.87 1128.31
4656 201703 284.18 1412.49
4656 201704 282.37 1694.86


 

sasuser_sk
Quartz | Level 8

Thank you, Reeza.

maguiremq
SAS Super FREQ

I like @Reeza's better, but here's how I did it before I saw their solution.

data want;
	set have;
	by id;
	retain total_amount;
		if first.id then total_amount = amount;
			else if not first.id then total_amount + amount;
run;
id 	month 	amount 	total_amount
1679 	201611 	16.47 	16.47
1679 	201612 	1.95 	18.42
1679 	201701 	-1.95 	16.47
1679 	201702 	188.56 	205.03
4656 	201611 	287.52 	287.52
4656 	201612 	277.92 	565.44
4656 	201701 	285.00 	850.44
4656 	201702 	277.87 	1128.31
4656 	201703 	284.18 	1412.49
4656 	201704 	282.37 	1694.86
sasuser_sk
Quartz | Level 8

Thank you for another example! I see that you used else if instead.

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
  • 4 replies
  • 1404 views
  • 2 likes
  • 3 in conversation