DATA Step, Macro, Functions and more

How to add a row level total to a SAS dataset

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,041
Accepted Solution

How to add a row level total to a SAS dataset

[ Edited ]

 

Hello,

 

I got the following output after performing a frequency procedure and transposing by month.

Later, i want to create a grand_total on the resultant dataset like shown.

is it acheived by using an array?

 

 

Month      Jan    Feb  mar  apr  may  jun   jul   grand_total

Male         6          4    3      8       8     0     .        29  

Female    4           4    7      7       0     .      .        22

 

Thanks


Accepted Solutions
Solution
‎04-15-2017 08:00 PM
Super User
Posts: 19,768

Re: How to add a row level total to a SAS dataset

Posted in reply to robertrao

Either use a reporting procedure from the start or calculate sum manually, but an array declaration isn't useful in this situation unless you're going to use it more than once or if you need some dynamic summation.  

 

You can use variable lists without explicitly declaring an array. 

 

Grand_total = sum (of JAN--JUL);

Grand_total = sum(of month1-month7);

View solution in original post


All Replies
Super Contributor
Posts: 438

Re: Array?

Posted in reply to robertrao

No need for arrays just use the sum function e.g.

 

grand_total=sum(jan, feb, mar, apr, may, jun, jul);

Solution
‎04-15-2017 08:00 PM
Super User
Posts: 19,768

Re: How to add a row level total to a SAS dataset

Posted in reply to robertrao

Either use a reporting procedure from the start or calculate sum manually, but an array declaration isn't useful in this situation unless you're going to use it more than once or if you need some dynamic summation.  

 

You can use variable lists without explicitly declaring an array. 

 

Grand_total = sum (of JAN--JUL);

Grand_total = sum(of month1-month7);
Super Contributor
Posts: 1,041

Re: How to add a row level total to a SAS dataset

Thanks Reeza,

 

The double dashes works fine like shown below in a seperrate data step after the proc transpose.

But putting a single dash inbetween doent work only considers the two months.

 

Grand_total = sum(of Feb2016--Feb2017);

 

Thanks again

 

Super User
Posts: 19,768

Re: How to add a row level total to a SAS dataset

Posted in reply to robertrao

You can only use single dashes if you have a series of variables with an index, as in the example. And I believe this behaviour changed in 9.3

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 243 views
  • 5 likes
  • 3 in conversation