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

Hi...I am trying to do a running total for each amount. The BegRevenue is the previous records EndRevenue and the current record's EndRevenue is the records BegRevenue plus the Show Amount. I can't seem to get this to work. Any suggestions....thanks.

 

data have;
    length ShowAmount 8 ;
    format ShowAmount Dollar20.2 ;
    informat ShowAmount Dollar20. ;
    infile datalines4 dlm='7F'x missover dsd;
    input ShowAmount: best32. ;
datalines4;
1550
-1550
1550
-1550
-1550
1550
1775
975
100
-1850
-900
-100
;;;;

data want(drop=_EndRevenue);
	set have;
	retain _EndRevenue;
		if _n_=1 then do;
			BegRevenue = 0;
			EndRevenue = ShowAmount;
			_EndRevenue = EndRevenue;
			end;
		else do;
			BegRevenue = _EndRevenue;
			EndRevenue = (BegRevenue+ShowAmount);
			BegRevenue = _EndRevenue;
			end;
run;
ShowAmount	BegRevenue	EndRevenue
$1,550.00	0	1550
-$1,550.00	1550	0
$1,550.00	1550	3100
-$1,550.00	1550	0
-$1,550.00	1550	0
$1,550.00	1550	3100
$1,775.00	1550	3325
$975.00	1550	2525
$100.00	1550	1650
-$1,850.00	1550	-300
-$900.00	1550	650
-$100.00	1550	1450
1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

I presume you are showing the numbers you got, instead of what you want.  Here is a program that generates what I understand to be your objective:

 

data have;
    length ShowAmount 8 ;
    format ShowAmount Dollar20.2 ;
    informat ShowAmount Dollar20. ;
    infile datalines4 dlm='7F'x missover dsd;
    input ShowAmount: best32. ;
datalines4;
1550
-1550
1550
-1550
-1550
1550
1775
975
100
-1850
-900
-100
;;;;

data want;
  set have;
  retain begrevenue 0;
  endrevenue=begrevenue+showamount;
  output;
  begrevenue=endrevenue;
run;

 

The "trick" here is to output the current record, and then update begrevenue in preparation for the next record.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

I presume you are showing the numbers you got, instead of what you want.  Here is a program that generates what I understand to be your objective:

 

data have;
    length ShowAmount 8 ;
    format ShowAmount Dollar20.2 ;
    informat ShowAmount Dollar20. ;
    infile datalines4 dlm='7F'x missover dsd;
    input ShowAmount: best32. ;
datalines4;
1550
-1550
1550
-1550
-1550
1550
1775
975
100
-1850
-900
-100
;;;;

data want;
  set have;
  retain begrevenue 0;
  endrevenue=begrevenue+showamount;
  output;
  begrevenue=endrevenue;
run;

 

The "trick" here is to output the current record, and then update begrevenue in preparation for the next record.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
twildone
Pyrite | Level 9
Hi..Mkeintz….Yes you are right. That is what I was wanting...thanks for your help...

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
  • 2 replies
  • 822 views
  • 1 like
  • 2 in conversation