<?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: tabulate and freq in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/tabulate-and-freq/m-p/611747#M178362</link>
    <description>&lt;P&gt;Usually for indicator variables you are only interested in YES and it is easy to get that with PROC MEANS; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input id reason_1-reason_3;
	cards;
1 1 1 0
2 1 0 0
3 0 1 0
4 0 1 1
5 . . .
6 1 0 0
7 1 1 0
8 0 1 1
9 0 0 1
10 1 1 0
;;;;
   run;
ods select none;
proc means data=have n sum mean stackods;
   var reason:;
   ods output summary=summary;
   run;
ods select all;
proc print data=summary;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 250px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34770i890050C6E455FE7B/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 13 Dec 2019 21:59:12 GMT</pubDate>
    <dc:creator>data_null__</dc:creator>
    <dc:date>2019-12-13T21:59:12Z</dc:date>
    <item>
      <title>tabulate and freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/tabulate-and-freq/m-p/611720#M178343</link>
      <description>&lt;P&gt;I have the following table:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;reason_1&lt;/TD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;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to assign 1=yes, and 0= no and then calculate frequency and count by each reason by category&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;no_count&lt;/TD&gt;&lt;TD&gt;no_pct&lt;/TD&gt;&lt;TD&gt;yes_count&lt;/TD&gt;&lt;TD&gt;yes_percent&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;reason_1&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;0.444444&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;55.6%&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;reason_2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;0.333333&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;66.7%&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;reason_3&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;0.666667&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;33.3%&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried the following:&lt;/P&gt;&lt;P&gt;proc freq data=test;&lt;/P&gt;&lt;P&gt;tables reason1 reason2 reason3;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 20:05:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/tabulate-and-freq/m-p/611720#M178343</guid>
      <dc:creator>radhikaa4</dc:creator>
      <dc:date>2019-12-13T20:05:46Z</dc:date>
    </item>
    <item>
      <title>Re: tabulate and freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/tabulate-and-freq/m-p/611723#M178345</link>
      <description>&lt;P&gt;Transpose to a long format, filter out observations where col1 = 0, and run proc freq on _name_.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 20:10:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/tabulate-and-freq/m-p/611723#M178345</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-13T20:10:54Z</dc:date>
    </item>
    <item>
      <title>Re: tabulate and freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/tabulate-and-freq/m-p/611724#M178346</link>
      <description>&lt;P&gt;PS you should also start studying the many answers you got to similar questions in your other threads, as you might already have a proper answer there.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 20:16:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/tabulate-and-freq/m-p/611724#M178346</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-13T20:16:14Z</dc:date>
    </item>
    <item>
      <title>Re: tabulate and freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/tabulate-and-freq/m-p/611725#M178347</link>
      <description>&lt;P&gt;Try this and just transpose the stats output on your own.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input id reason_1 reason_2 reason_3;
	cards;
1 1 1 0
2 1 0 0
3 0 1 0
4 0 1 1
5 . . .
6 1 0 0
7 1 1 0
8 0 1 1
9 0 0 1
10 1 1 0
run;

proc sort data=have;
	by id;
run;

proc transpose data=have out=t(rename=(_name_=reason col1=answer));
	by id;
run;

proc sort data=t;
	by reason;
run;

proc freq data=t noprint;
	by reason;
	tables reason * answer / out=stats(where=(not missing(answer)));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Dec 2019 20:18:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/tabulate-and-freq/m-p/611725#M178347</guid>
      <dc:creator>jvdl</dc:creator>
      <dc:date>2019-12-13T20:18:52Z</dc:date>
    </item>
    <item>
      <title>Re: tabulate and freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/tabulate-and-freq/m-p/611732#M178353</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/153469"&gt;@radhikaa4&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can try this, using a proc format to specify Yes / No instead of 1 / 0:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input id reason_1 reason_2 reason_3;
	cards;
1 1 1 0
2 1 0 0
3 0 1 0
4 0 1 1
5 . . .
6 1 0 0
7 1 1 0
8 0 1 1
9 0 0 1
10 1 1 0
;
run;

proc format fmtlib;
	value reason 
		0 = "No"
		1 = "Yes";
run;

data want;
	set have;
	format value reason.;
	by id;
	array reason_(3);
	do i=1 to dim(reason_);
		reason = vname(reason_(i));
		value = reason_(i);
		output;
	end;
	drop i reason_:;
run;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;Output using proc freq:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=want;
	tables reason * value / nocum nocol nopercent;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Capture d’écran 2019-12-13 à 22.28.47.png" style="width: 271px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34766i8A711C29E29E4E25/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture d’écran 2019-12-13 à 22.28.47.png" alt="Capture d’écran 2019-12-13 à 22.28.47.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;or output using proc report:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc report data=want;
	column reason value, (n pct);
	define reason / group 'Reason';
	define value / across;
	define n / "n" ;
	define pct / computed "%" f=percent8.0;
	
	/* Sum totals in row for further calculation */
	compute before reason;
 	    _total = sum(_c2_, _c4_);
    endcomp;
    
    /* Compute percentages from frequencies (n) and totals in row (_total) */
    compute pct;
		if _c2_&amp;gt; 0 then _c3_ = _c2_ / _total;
		else _c3_= 0; 
		if _c4_&amp;gt; 0 then _c5_ = _c4_ / _total;
		else _c5_= 0; 
	endcomp;

;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Capture d’écran 2019-12-13 à 22.28.43.png" style="width: 175px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34767iD7144BB497BBFC89/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture d’écran 2019-12-13 à 22.28.43.png" alt="Capture d’écran 2019-12-13 à 22.28.43.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 21:29:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/tabulate-and-freq/m-p/611732#M178353</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-13T21:29:48Z</dc:date>
    </item>
    <item>
      <title>Re: tabulate and freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/tabulate-and-freq/m-p/611747#M178362</link>
      <description>&lt;P&gt;Usually for indicator variables you are only interested in YES and it is easy to get that with PROC MEANS; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input id reason_1-reason_3;
	cards;
1 1 1 0
2 1 0 0
3 0 1 0
4 0 1 1
5 . . .
6 1 0 0
7 1 1 0
8 0 1 1
9 0 0 1
10 1 1 0
;;;;
   run;
ods select none;
proc means data=have n sum mean stackods;
   var reason:;
   ods output summary=summary;
   run;
ods select all;
proc print data=summary;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 250px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34770i890050C6E455FE7B/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 21:59:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/tabulate-and-freq/m-p/611747#M178362</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2019-12-13T21:59:12Z</dc:date>
    </item>
    <item>
      <title>Re: tabulate and freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/tabulate-and-freq/m-p/611783#M178389</link>
      <description>&lt;P&gt;EDITED.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input id reason_1-reason_3;
	cards;
1 1 1 0
2 1 0 0
3 0 1 0
4 0 1 1
5 . . .
6 1 0 0
7 1 1 0
8 0 1 1
9 0 0 1
10 1 1 0
;;;;
proc sql;
create table want as
select 'reason_1 ' as reason,
sum(reason_1=0) as no_count,calculated no_count/count(reason_1) as no_percent ,
sum(reason_1=1) as yes_count,calculated yes_count/count(reason_1) as yes_percent
from have
union
select 'reason_2 ' as reason,
sum(reason_2=0) as no_count,calculated no_count/count(reason_2) as no_percent,
sum(reason_2=1) as yes_count,calculated yes_count/count(reason_2) as yes_percent
from have
union
select 'reason_3 ' as reason,
sum(reason_3=0) as no_count,calculated no_count/count(reason_3) as no_percent,
sum(reason_3=1) as yes_count,calculated yes_count/count(reason_3) as yes_percent
from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 14 Dec 2019 10:13:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/tabulate-and-freq/m-p/611783#M178389</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-12-14T10:13:24Z</dc:date>
    </item>
  </channel>
</rss>

