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

Calculating percentages with the N of a row as the denominator. 

 

What I want to do is create a table using proc tabulate, if possible, where the N of the row is used as the denominator for the cell.

 

Quarter is a class variable, with categories of Q1, Q2, Q3, Q4 (this is to be used as the row). Flag_1 and Flag_2 are binary flag variables; they have values of either 1 or 0. 

 

Basically, I want the sum of my flag variables (flag_1 and flag_2) within a row (quarter class variable) to be the numerator in a cell, divided by the number of observations for that row, where the quarter is Q1 etc. 

 

The desired output is below (Made up numbers for the sum of observations in a row (30, 40, etc); wanted to emphasize that the number of observations with a different quarter vary): 

_UAG24T3.png

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

sum of var divided by number of obs is called the mean.

 


data HAVE;
  do QUARTER=1 to 4;
    do FL1=0 to 1;
      do FL2=0 to 1;
        do I=1 to 10;
          if ranuni(0)>.5 then output;
        end;
      end;
    end;
  end;
run;

proc tabulate data=HAVE;
  class QUARTER ;
  var FL1 FL2;
  table QUARTER, n (FL1 FL2)*(sum mean) ;
run;
 

 

 

  N FL1 FL2
Sum Mean Sum Mean
QUARTER 18 11.00 0.61 9.00 0.50
1
2 23 13.00 0.57 14.00 0.61
3 18 9.00 0.50 9.00 0.50
4 21 8.00 0.38 10.00 0.48

 

 

 

View solution in original post

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

sum of var divided by number of obs is called the mean.

 


data HAVE;
  do QUARTER=1 to 4;
    do FL1=0 to 1;
      do FL2=0 to 1;
        do I=1 to 10;
          if ranuni(0)>.5 then output;
        end;
      end;
    end;
  end;
run;

proc tabulate data=HAVE;
  class QUARTER ;
  var FL1 FL2;
  table QUARTER, n (FL1 FL2)*(sum mean) ;
run;
 

 

 

  N FL1 FL2
Sum Mean Sum Mean
QUARTER 18 11.00 0.61 9.00 0.50
1
2 23 13.00 0.57 14.00 0.61
3 18 9.00 0.50 9.00 0.50
4 21 8.00 0.38 10.00 0.48

 

 

 
renalstats
Fluorite | Level 6
Thank you, I was looking/thinking about it incorrectly.
ballardw
Super User

@renalstats wrote:

Calculating percentages with the N of a row as the denominator. 

 

What I want to do is create a table using proc tabulate, if possible, where the N of the row is used as the denominator for the cell.

 

Quarter is a class variable, with categories of Q1, Q2, Q3, Q4 (this is to be used as the row). Flag_1 and Flag_2 are binary flag variables; they have values of either 1 or 0. 

 

Basically, I want the sum of my flag variables (flag_1 and flag_2) within a row (quarter class variable) to be the numerator in a cell, divided by the number of observations for that row, where the quarter is Q1 etc. 

 

The desired output is below (Made up numbers for the sum of observations in a row (30, 40, etc); wanted to emphasize that the number of observations with a different quarter vary): 

_UAG24T3.png

 

 


Your data picture shows a sum/sum your description says a sum divided by an n (number of observations). so which is it you want?

 

Proc tabulate has statistics colpctsum and rowpctsum if the denominatior is a sum.

 

You much better off providing an example data set in data step form as @ChrisNZ did and then actual numbers in your result so we can check code behavior.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 2265 views
  • 2 likes
  • 3 in conversation