SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2982 views
  • 2 likes
  • 3 in conversation