DATA Step, Macro, Functions and more

Adding a sum to the top of dataset

Accepted Solution Solved
Reply
Frequent Learner CDR
Frequent Learner
Posts: 1
Accepted Solution

Adding a sum to the top of dataset

Hi

 

I'm trying to add a sum to my data set. I want it to be in the first line - just under the lables.

 

I have:

                   Num1       Num2

name1             12           10

name1              8             6

name1              9            11

 

I want:

                   Num1       Num2

Total                29           27

name1             12           10

name1              8             6

name1              9            11

 

Thanks in advance Smiley Happy

/Christine


Accepted Solutions
Solution
‎12-29-2017 03:55 AM
Super User
Super User
Posts: 9,227

Re: Adding a sum to the top of dataset

proc sql;
  create table WANT as 
  select  "Total" as NAME,
          sum(NUM1) as NUM1,
          sum(NUM2) as NUM2
  from    HAVE
  union all
  select  NAME,
          NUM1,
          NUM2
  from    HAVE;
quit;

View solution in original post


All Replies
Solution
‎12-29-2017 03:55 AM
Super User
Super User
Posts: 9,227

Re: Adding a sum to the top of dataset

proc sql;
  create table WANT as 
  select  "Total" as NAME,
          sum(NUM1) as NUM1,
          sum(NUM2) as NUM2
  from    HAVE
  union all
  select  NAME,
          NUM1,
          NUM2
  from    HAVE;
quit;
Super Contributor
Posts: 500

Re: Adding a sum to the top of dataset

Do you want the sums in a dataset or report?

 

Proc report and proc tabulate allow sum-rows before and after details-rows.

 

proc tabulate data=sashelp.class;    
   var height weight;
   class sex / order=unformatted missing;
   table 
      all={label="Total"} sex='',
      height weight
   ;
   keylabel sum= ' ';
run;
☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 155 views
  • 5 likes
  • 3 in conversation