DATA Step, Macro, Functions and more

Calculating totals

Accepted Solution Solved
Reply
Contributor
Posts: 53
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
Regular Contributor
Posts: 234

Re: Calculating totals

[ Edited ]
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,610

Re: Calculating totals

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: 53

Re: Calculating totals

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: 252

Re: Calculating totals

[ Edited ]

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,610

Re: Calculating totals


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: 53

Re: Calculating totals

Thank you I will try it.
Solution
‎07-17-2017 05:17 PM
Regular Contributor
Posts: 234

Re: Calculating totals

[ Edited ]
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: 53

Re: Calculating totals

Thank you!..It worked!
Super User
Posts: 17,784

Re: Calculating totals

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: 53

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
  • 251 views
  • 0 likes
  • 5 in conversation