BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
robertrao
Quartz | Level 8

 

 

I want the output like shown below:What changes do i need to make to the PROC TABULATE code:

 

Proc tabulate data=have;

class source;

var A B C D E;

table (A B C D E ) *source,

sum=

n=

mean=

;

run;

 

 

OUTPUT:

 

 

                       Source1                   Source2

 

               sum N mean              Sum N Mean

A

B

C

D

E

Overall --- --- -----                    ---    ---    -----

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@robertrao wrote:

I see. How about using Proc Report instead of tabulate. Can be acheived?

 

Thanks


I think Proc Report would do the calculation if you are willing to have the A B C as columns and the Source as rows.

I don't use Report often and there are a number of headaches with the way cross-column calculations are done I have to be really desparate to go there when I have an easier way.

 

It would take about 7 lines of code to make the A B C category data set similar to the one you provided earlier with the variables (or the variable labels for A B C which would likely be better) as a categorical variable.

View solution in original post

12 REPLIES 12
Shmuel
Garnet | Level 18

Please supply some test data in a format of dataset with datalines (or cards).

 

 

Reeza
Super User

Test it until you get what you want. You can't break it...,

 

Try

 

Table (A b c d e all), source*(n mean sum std);

As indicated you should provide either test data or use the relevant SASHELP dataset.

In this case, the SHOES dataset is probably closest to your actual structure. But that's really a guess at this point in time.  

ballardw
Super User

If you want "overall" to sum the A B C values tabulate does not work that way.

You would have to do the sum in the data set as a separate value. And the "mean" definition may get a bit tricky especially if you are continuing your previous question about getting counts and percents of your "unwanted" values.

robertrao
Quartz | Level 8

data temp;

input meas$ source$ surgery;

datalines;

MEAS_A hosp1 1

MEAS_A hosp1 1

MEAS_A hosp1 1

MEAS_A hosp1 1

MEAS_A hosp1 1

MEAS_A hosp1 1

MEAS_A hosp1 1

MEAS_A hosp1 1

MEAS_A hosp2 0

MEAS_A hosp2 1

MEAS_A hosp2 1

MEAS_A hosp2 1

MEAS_A hosp2 1

MEAS_A hosp2 1

MEAS_A hosp2 1

MEAS_A hosp2 1

MEAS_A hosp2 1

MEAS_A hosp2 1

MEAS_A hosp2 1

MEAS_A hosp2 1

MEAS_A hosp2 1

MEAS_A hosp2 1

MEAS_A hosp2 1

MEAS_A hosp2 1

MEAS_A hosp2 1

MEAS_A hosp2 1

MEAS_A hosp2 1

MEAS_B hosp1 1

MEAS_B hosp1 0

MEAS_B hosp1 0

 

 

MEAS_B hosp2 1

MEAS_B hosp2 1

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

MEAS_B hosp2 0

;

run;

 

 

 

 

 

I want the output like shown below:What changes do i need to make to the PROC TABULATE code:

 

Proc tabulate data=have;

class source;

var A B ;

table (A B ) *source,

sum=

n=

mean=

;

run;

 

 

OUTPUT:

 

 

                       hosp1                                                  hosp2

 

                   sum N   mean(%)                           Sum N      Mean(%)

MEAS A        8    8     8/8*100=100%                 18   19      18/19*100=94.74%

MEASB         1     3    1/3*100=33.3%                2     28      2/28*100=7.14%     

Overall          9     11   9/11*100= 81.81%        20   47    20/47*100=42.55%

 

Reeza
Super User

Why didn't you start with this? And why are there still A B in proc tabulate? And what's actually your Var? The code doesn't line up with your data. 

 

And typing this took the same amount of time as typing an answer would have taken. 

robertrao
Quartz | Level 8

Hello,

 

I am sorry for the confusion. A and B should have been MEAS A and MEAS B respectively.(forgot to change the old code which was suggested by Ballard and Art yesterday).

Shortly after I posted the question I figured out from the article that all the stats have to be in braces to split the results for each of the values under class variable.

http://support.sas.com/resources/papers/proceedings11/173-2011.pdf

 

Lastly, i could not start with this yesterday since I was asked to split the report after I started working on it..

 

Hope you will understand..Also I am working on other tools these days so lost the little knowledge I learnt

 

Thanks

 

 

ballardw
Super User
proc tabulate data= temp;
   class meas source;
   var surgery;
   table meas='' all="Overall",
         source='' * surgery='' *( sum n mean="%"*f=percent8.1)
   ;
run;

Note that your original question was showing multiple variable instead of one variable with multiple levels (which is what tabulate works with). the misc ' ' are just to suprees variable names since you didn't provide or indicate a desire for more specific labels other than for mean.

 

robertrao
Quartz | Level 8

Thanks Ballard...

 

I need it for multiple variables... That example dataset i provided was a mistake. Can your code be expanded to accomodate for multiple variable ?

 

Thanks

ballardw
Super User

@robertrao wrote:

Thanks Ballard...

 

I need it for multiple variables... That example dataset i provided was a mistake. Can your code be expanded to accomodate for multiple variable ?

 

Thanks


No. Proc tabulate will not sum across other variables.

However you can create a class variable whose values are the names of the other variables and use the example code.

robertrao
Quartz | Level 8

I see. How about using Proc Report instead of tabulate. Can be acheived?

 

Thanks

ballardw
Super User

@robertrao wrote:

I see. How about using Proc Report instead of tabulate. Can be acheived?

 

Thanks


I think Proc Report would do the calculation if you are willing to have the A B C as columns and the Source as rows.

I don't use Report often and there are a number of headaches with the way cross-column calculations are done I have to be really desparate to go there when I have an easier way.

 

It would take about 7 lines of code to make the A B C category data set similar to the one you provided earlier with the variables (or the variable labels for A B C which would likely be better) as a categorical variable.

Reeza
Super User

@robertrao wrote:

I see. How about using Proc Report instead of tabulate. Can be acheived?

 

Thanks


Or make your data look like it has one variable by using PROC TRANSPOSE. Then you can use the same idea. 

sas-innovate-2024.png

Today is the last day to save with the early bird rate! Register today for just $695 - $100 off the standard rate.

 

Plus, pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 12 replies
  • 1927 views
  • 2 likes
  • 4 in conversation