Totals

Accepted Solution Solved
Reply
Contributor
Posts: 42
Accepted Solution

Totals

I am a novice with SAS and am trying to create a simple column total.  I have read past discussions in SAS but still have not gotten what I need.  I have tried to use both Proc SQL and Proc Print to accomplish this task.  Any help is appreciated.

I have the following data:

ID                  Charge

1                    $15,000

2                    $20,000

3                    $35,000

4                    $5,000

5                    $2000

I want:

ID                  Charge

1                    $15,000

2                    $20,000

3                    $35,000

4                    $5,000

5                    $2000

Total              $77,000

I tried the following codes:

proc sql;

create table charges2 as

select t1.id, 

t1.charge_amt, 

sum(t1.charge_amt) as totalcharges

from work.charges as t1

group by id;

quit;

Also tried:

data charges;

keep id charge_amt;

set input;

run;

proc print data=charges noobs;

sum charge_amt;

by id;

run;


Accepted Solutions
Solution
‎03-13-2015 03:04 PM
Frequent Contributor
Posts: 115

Re: Totals

data want;

set have;

total+charge;

run;

Your example doesn't seem like a group by summarization?

View solution in original post


All Replies
Solution
‎03-13-2015 03:04 PM
Frequent Contributor
Posts: 115

Re: Totals

data want;

set have;

total+charge;

run;

Your example doesn't seem like a group by summarization?

Contributor
Posts: 42

Re: Totals

Thank you very much for your help!  It gave me a running total.

Contributor
Posts: 42

Re: Totals

To confirm, the total+1 acts as a running total?  It keeps the value of the previous result and adds to the next, so on and so forth?

Frequent Contributor
Posts: 115

Re: Totals

Yes indeed, that's the functionality of sum statement, which retains across observation and assigns automatically an initial value of 0 for the first obs.

Definition: Adds the result of an expression to an accumulating variable.

HTH,

Naveen Srinivasan

L&T Infotech

Frequent Contributor
Posts: 115

Re: Totals

proc print data=have;

sum charge;

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 313 views
  • 0 likes
  • 2 in conversation