<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to divide counts within proc freq in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652035#M195706</link>
    <description>&lt;P&gt;If you recode your data such that 1 is 1, 0 are missing and anything &amp;gt;1 is 0 then this can be done with proc tabulate or report quite nicely.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 31 May 2020 00:29:48 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2020-05-31T00:29:48Z</dc:date>
    <item>
      <title>How to divide counts within proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652024#M195696</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I have a question about dividing counts of values generated from proc freq. I have a dataset that looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data my_data;&lt;BR /&gt;input id type$ option1 option2 option3;&lt;BR /&gt;cards;&lt;BR /&gt;1 A 1 0 2&lt;BR /&gt;2 A 1 1 1&lt;BR /&gt;3 B 0 3 2&lt;BR /&gt;4 B 2 0 0&lt;BR /&gt;5 B 0 1 2&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to do this same calculation for EACH option: (count of number of 1's) / (count of number of &amp;gt;=1's). The denominator does not include the 0 values.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So for option1, I would want some kind of output that tells me 2/3. Option2 = 2/3. Option3=1/4.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In addition to generating these percentages for the overall cohort of individuals, I also want to be able to generate these values BY the "type" variable (for type=A only and type=B only).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried playing around with proc freq with an out= statement, as well as proc report, but I can't think of an easy way to generate these numbers.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance for your help!&lt;/P&gt;</description>
      <pubDate>Sat, 30 May 2020 22:12:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652024#M195696</guid>
      <dc:creator>telc24</dc:creator>
      <dc:date>2020-05-30T22:12:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to divide counts within proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652025#M195697</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data my_data;
input id type$ option1 option2 option3;
cards;
1 A 1 0 2
2 A 1 1 1
3 B 0 3 2
4 B 2 0 0
5 B 0 1 2
;
run;

proc transpose data=my_data out=temp;
by id ;
var option1-option3;
run;

proc sql;
create table want as
select _name_ as option label=' ', sum(col1=1)/sum(col1&amp;gt;=1) as ratio
from temp
group by option;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure SQL: Query Results" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l b header" scope="col"&gt;option&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;ratio&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;option1&lt;/TD&gt;
&lt;TD class="r data"&gt;0.666667&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;option2&lt;/TD&gt;
&lt;TD class="r data"&gt;0.666667&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;option3&lt;/TD&gt;
&lt;TD class="r data"&gt;0.25&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Sat, 30 May 2020 22:22:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652025#M195697</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-30T22:22:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to divide counts within proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652026#M195698</link>
      <description>&lt;P&gt;Thank you so much, this worked perfectly!! Would it be possible to add two more columns for the ratio for individuals with type=A and the ratio for type=B?&lt;/P&gt;</description>
      <pubDate>Sat, 30 May 2020 22:40:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652026#M195698</guid>
      <dc:creator>telc24</dc:creator>
      <dc:date>2020-05-30T22:40:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to divide counts within proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652033#M195704</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/125447"&gt;@telc24&lt;/a&gt;&amp;nbsp; Can you please post the structure of the output table that you&amp;nbsp;&lt;SPAN&gt;want it to look like?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 31 May 2020 00:08:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652033#M195704</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-31T00:08:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to divide counts within proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652034#M195705</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;. Sure, I was thinking something like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Option&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Overall_ratio&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;TypeA_ratio&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;TypeB_ratio&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Option1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0.666667&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Option2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0.666667&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0.5&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Option3&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0.25&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0.5&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Alternatively, I could use the existing code you provided (to calculate the overall_ratio) and replicate it in two subsetted datasets (for type=A and type=B individuals).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you again for your help!&lt;/P&gt;</description>
      <pubDate>Sun, 31 May 2020 00:16:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652034#M195705</guid>
      <dc:creator>telc24</dc:creator>
      <dc:date>2020-05-31T00:16:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to divide counts within proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652035#M195706</link>
      <description>&lt;P&gt;If you recode your data such that 1 is 1, 0 are missing and anything &amp;gt;1 is 0 then this can be done with proc tabulate or report quite nicely.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 May 2020 00:29:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652035#M195706</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-05-31T00:29:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to divide counts within proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652039#M195709</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data my_data;
input id type$ option1 option2 option3;
cards;
1 A 1 0 2
2 A 1 1 1
3 B 0 3 2
4 B 2 0 0
5 B 0 1 2
;
run;

proc transpose data=my_data out=temp;
by id type;
var option1-option3;
run;

proc sql;
create table temp1 as
select a.*, Overall_ratio
from
(select type,_name_ as option label=' ', sum(col1=1)/sum(col1&amp;gt;=1) as ratio
from temp
group by type,option) a
inner join
(select _name_ as option label=' ', sum(col1=1)/sum(col1&amp;gt;=1) as Overall_ratio
from temp
group by option)b
on a.option=b.option
order by option,type;
quit;

proc transpose data=temp1 out=want(drop=_name_) prefix=Type suffix=_ratio;
by option overall_ratio;
var ratio;
id type;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 31 May 2020 00:41:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652039#M195709</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-31T00:41:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to divide counts within proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652040#M195710</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;thank you!! Couldn't quite get it to work, but the original code is extremely helpful and is much easier than the alternative method I was using before. Thank you for all of your time and assistance!&lt;/P&gt;</description>
      <pubDate>Sun, 31 May 2020 01:00:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652040#M195710</guid>
      <dc:creator>telc24</dc:creator>
      <dc:date>2020-05-31T01:00:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to divide counts within proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652041#M195711</link>
      <description>&lt;P&gt;Hi again, The above SQL can be made terse by taking advantage of the automatic remerge that the SQL optimizer utlizes. Guru&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp; uses auto remerge a lot as opposed to explicit join perhaps he prefers to avoid the verbosity. My kno&lt;SPAN&gt;wlege is a drop in his ocean though I'm good in plagiarizing his solutions. So his SQL autoremerge approach&amp;nbsp;would likely be&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table temp1 as
select _name_ as option label=' ',type,Overall_ratio, sum(col1=1)/sum(col1&amp;gt;=1) as ratio
from 
(select *, sum(col1=1)/sum(col1&amp;gt;=1) as Overall_ratio
from temp
group by option)
group by option,type,Overall_ratio
order by option,type;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Yet another dorfmanism that performs surgery in sas.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data my_data;
input id type$ option1 option2 option3;
cards;
1 A 1 0 2
2 A 1 1 1
3 B 0 3 2
4 B 2 0 0
5 B 0 1 2
;
run;

proc transpose data=my_data out=temp;
by id type;
var option1-option3;
run;

proc sql;
create table temp1 as
select _name_ as option label=' ',type,Overall_ratio, sum(col1=1)/sum(col1&amp;gt;=1) as ratio
from 
(select *, sum(col1=1)/sum(col1&amp;gt;=1) as Overall_ratio
from temp
group by option)
group by option,type,Overall_ratio
order by option,type;
quit;

proc transpose data=temp1 out=want1(drop=_name_) prefix=Type suffix=_ratio;
by option overall_ratio;
var ratio;
id type;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="results.PNG" style="width: 365px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/40179i0CBE4F062AE2FE30/image-size/large?v=v2&amp;amp;px=999" role="button" title="results.PNG" alt="results.PNG" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 31 May 2020 01:06:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652041#M195711</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-31T01:06:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to divide counts within proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652043#M195712</link>
      <description>&lt;P&gt;Thank you!! I'll give this a try!&lt;/P&gt;</description>
      <pubDate>Sun, 31 May 2020 01:10:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652043#M195712</guid>
      <dc:creator>telc24</dc:creator>
      <dc:date>2020-05-31T01:10:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to divide counts within proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652044#M195713</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;! Clever solution, wish I thought of it myself!&lt;/P&gt;</description>
      <pubDate>Sun, 31 May 2020 01:11:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-divide-counts-within-proc-freq/m-p/652044#M195713</guid>
      <dc:creator>telc24</dc:creator>
      <dc:date>2020-05-31T01:11:40Z</dc:date>
    </item>
  </channel>
</rss>

