DATA Step, Macro, Functions and more

Calculating totals

Accepted Solution Solved
Reply
Contributor
Posts: 70
Accepted Solution

Calculating totals

I don't want to try Proc Sql;

 I am trying to get the aggregated total for the new column.

 

data test.id;
set test.data;

Total_amt=sum(d_amt);

drop d_amt;
run;

 

Current Output                                Actual Output

Acct  Total amt                                Acct  Total amt

23       400                                      23     1000

23       600

 


Accepted Solutions
Solution
‎07-17-2017 05:17 PM
Super Contributor
Posts: 271

Re: Calculating totals

[ Edited ]
Posted in reply to Kalai2008
data have;
input Acct amt;
cards; 
23 400
23 600
;
run;

data want;
  do until(last.Acct);
  	set have;
	by Acct;
	total_amt = sum(total_amt,amt);
  end;
  output;
  drop amt;
run;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,918

Re: Calculating totals

Posted in reply to Kalai2008

PROC MEANS or PROC SUMMARY will find the totals for you.

 

Use the CLASS statement if you want to get totals by ACCT.

Contributor
Posts: 70

Re: Calculating totals

Posted in reply to PaigeMiller

I dont want to try it in Proc means or Proc Summary.... Trying to build a new data set ..so I need to do it in data step.

PROC Star
Posts: 325

Re: Calculating totals

[ Edited ]
Posted in reply to Kalai2008

you could use output =dataset in proc means or summary as suggested @PaigeMiller. Please check the below paper for reference.

 

http://www2.sas.com/proceedings/sugi27/p018-27.pdf

 

Trusted Advisor
Posts: 1,918

Re: Calculating totals

Posted in reply to Kalai2008

Kalai2008 wrote:

I dont want to try it in Proc means or Proc Summary.... Trying to build a new data set ..so I need to do it in data step.


As stated by @kiranv_, both PROC MEANS and PROC SUMMARY produce output data sets.

Contributor
Posts: 70

Re: Calculating totals

Posted in reply to PaigeMiller
Thank you I will try it.
Solution
‎07-17-2017 05:17 PM
Super Contributor
Posts: 271

Re: Calculating totals

[ Edited ]
Posted in reply to Kalai2008
data have;
input Acct amt;
cards; 
23 400
23 600
;
run;

data want;
  do until(last.Acct);
  	set have;
	by Acct;
	total_amt = sum(total_amt,amt);
  end;
  output;
  drop amt;
run;
Contributor
Posts: 70

Re: Calculating totals

Posted in reply to SAS_inquisitive
Thank you!..It worked!
Super User
Posts: 19,802

Re: Calculating totals

Posted in reply to Kalai2008

https://github.com/statgeek/SAS-Tutorials/blob/master/proc_means_basic

 

*Create summary statistics for a dataset by a 'grouping' variable and store it in a dataset;

*Generate sample fake data;
data have;
	input ID          feature1         feature2         feature3;
	cards;
1               7.72               5.43              4.35
1               5.54               2.25              8.22 
1               4.43               6.75              2.22
1               3.22               3.21              7.31
2               6.72               2.86              6.11
2               5.89               4.25              5.25 
2               3.43               7.30              8.21
2               1.22               3.55              6.55

;
run;

*Create summary data;
proc means data=have noprint;
	by id;
	var feature1-feature3;
	output out=want median= var= mean= /autoname;
run;

*Show for display;
proc print data=want;
run;
Contributor
Posts: 70

Re: Calculating totals

Thank you..It worked
☑ This topic is solved.

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

Discussion stats
  • 9 replies
  • 256 views
  • 0 likes
  • 5 in conversation