Help using Base SAS procedures

summarize with subcolumns

Accepted Solution Solved
Reply
Regular Contributor
Posts: 192
Accepted Solution

summarize with subcolumns

Hi,

I have data that looks like this:

 

builidingname    vax1  vax2    vax3     buildingtype

happy days           1        2          4        private

learning tree          2       1          3        public

bean stalk              1      1          2        public

 

How do I summarize the info for all buildings as well as by buildingtype so I get the data like this:

      vax1                             vax2                            vax3

all   public private          all   public private       all   public private 

4       3         1               4         2        2             9      5         4

 

 

Thanks!

 


Accepted Solutions
Solution
‎02-27-2017 10:56 AM
Super User
Posts: 9,687

Re: summarize with subcolumns

@mkeintz sent me a private message. And I quote here.

 

 

You don't need data set WANT.  This is actually a good case to use multiple analysis vars in the proc tabulate:

 

proc tabulate data=have noseps order=freq;

  class buildingtype;

  var vax1-vax3;

  tables sum=''*(vax1 vax2 vax3)*(all buildingtype='')*f=9.0;

run;

 

 

Using the "order=freq" is a bit sneaky since it won't reliably order the table as (All,Public,Private) although it does in this case, as per OP's sample output.

View solution in original post


All Replies
Super User
Posts: 17,907

Re: summarize with subcolumns

Is this a report or a dataset that you need? I'm guesing report based on the structure.

 

Have you looked at proc report or tabulate? If so, what does your code look like so far?

I would probably recommend proc tabulate.

Super User
Posts: 9,687

Re: summarize with subcolumns

data have;
input buildingname & $20.   vax1  vax2    vax3     buildingtype $;
cards;
happy days           1        2          4        private
learning tree          2       1          3        public
bean stalk              1      1          2        public
;
run;
proc transpose data=have out=want;
by buildingname buildingtype notsorted;
var vax:;
run;
proc tabulate data=want;
 class _name_ buildingtype;
 var col1;
 table _name_=' '*(all buildingtype=' ')*col1=' '*sum=' '*f=best12.;
run;
Solution
‎02-27-2017 10:56 AM
Super User
Posts: 9,687

Re: summarize with subcolumns

@mkeintz sent me a private message. And I quote here.

 

 

You don't need data set WANT.  This is actually a good case to use multiple analysis vars in the proc tabulate:

 

proc tabulate data=have noseps order=freq;

  class buildingtype;

  var vax1-vax3;

  tables sum=''*(vax1 vax2 vax3)*(all buildingtype='')*f=9.0;

run;

 

 

Using the "order=freq" is a bit sneaky since it won't reliably order the table as (All,Public,Private) although it does in this case, as per OP's sample output.

Regular Contributor
Posts: 192

Re: summarize with subcolumns

Thank you!

Is it possible to output that into MS Excel instead of keeping it is SAS as HTML?

Super User
Posts: 9,687

Re: summarize with subcolumns

Sure.

 

ods excel file='c:\temp\x.xlsx';

proc tabulate............

..........

ods excel close;

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 184 views
  • 0 likes
  • 3 in conversation