<?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: Use of output from proc freq in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-of-output-from-proc-freq/m-p/906462#M357935</link>
    <description>&lt;P&gt;Thank you so much!&lt;/P&gt;</description>
    <pubDate>Wed, 06 Dec 2023 15:00:35 GMT</pubDate>
    <dc:creator>ANKH1</dc:creator>
    <dc:date>2023-12-06T15:00:35Z</dc:date>
    <item>
      <title>Use of output from proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-output-from-proc-freq/m-p/906369#M357890</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;I ran the following proc and got the data below.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc freq data=ds1;
table var1;
by time group;
run;

data output;
input time group var1 count percentage;
datalines;
0	1	1	3	42.8
0	1	2	4	57.14
0	2	1	7	58.3
0	2	2	5	41.6
3	1	1	2	28.6
3	1	2	5	71.5
3	2	1	1	50
3	2	2	1	50
6	1	2	3	100
6	2	1	2	100
9	1	2	2	100
9	2	1	2	66.6
9	2	2	2	33.3

;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Var1 is a categorical variable (1=yes, 2=no, 3=unknown). My question is if there is a way to use proc freq or if a data step is what is needed to get the desired output. These are the points to consider:&lt;BR /&gt;1) This is data grouped by window and by group. The percentage for each category of var1 is calculated in the above proc freq by: (count of category/sum of counts with categories with answers)*100.&lt;BR /&gt;2) However, we need to calculate the percentages by taking into account that the sample is 12. That is for the first row, the percentage should be calculated by (count of category/12)*100= 25%.Second row the percentage should be 33.33%.&lt;BR /&gt;3) A third category should be created to account for the unknown in the sample size of 12 (12-(3+4)=5). That means for the first group, the percentages should be 1=25%, 2=33.3%, 3= 41.6%. But since since this step is just one before other data wrangling, new rows have to be created (all rows were var1=3):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want;
input time group var1 count percentage;
datalines;
0	1	1	3	25
0	1	2	4	33.33333333
0	1	3	5	41.66666667
0	2	1	7	58.33333333
0	2	2	5	41.66666667
0	2	3	0	0
3	1	1	2	16.66666667
3	1	2	5	41.66666667
3	1	3	5	41.66666667
3	2	1	1	8.333333333
3	2	2	1	8.333333333
3	2	3	10	83.33333333
6	1	2	3	25
6	1	3	9	75
6	2	1	2	16.66666667
6	2	3	10	83.33333333
9	1	2	2	16.66666667
9	1	3	10	83.33333333
9	2	1	2	16.66666667
9	2	2	2	16.66666667
9	2	3	8	66.66666667
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Dec 2023 01:01:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-output-from-proc-freq/m-p/906369#M357890</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-12-06T01:01:48Z</dc:date>
    </item>
    <item>
      <title>Re: Use of output from proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-output-from-proc-freq/m-p/906374#M357892</link>
      <description>PROC FREQ is not going to create observations that are not already there, which is what you are asking for in creating VAR1=3 frequencies.&amp;amp;nbsp;&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;You will need a DATA step:&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;data want (drop=_:);&lt;BR /&gt;  set output;&lt;BR /&gt;  by time group;&lt;BR /&gt;  percentage=100*(count/12);&lt;BR /&gt;  output;&lt;BR /&gt;&lt;BR /&gt;  retain _remaining 12;&lt;BR /&gt;  if first.group then _remaining=12-count;&lt;BR /&gt;  else _remaining=_remaining-count;&lt;BR /&gt;&lt;BR /&gt;  if last.group then do;&lt;BR /&gt;    var1=3;&lt;BR /&gt;    count=_remaining;&lt;BR /&gt;    percentage=100*(count/12);&lt;BR /&gt;    output;&lt;BR /&gt;  end;&lt;BR /&gt;run;</description>
      <pubDate>Wed, 06 Dec 2023 15:40:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-output-from-proc-freq/m-p/906374#M357892</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-12-06T15:40:53Z</dc:date>
    </item>
    <item>
      <title>Re: Use of output from proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-output-from-proc-freq/m-p/906462#M357935</link>
      <description>&lt;P&gt;Thank you so much!&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2023 15:00:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-output-from-proc-freq/m-p/906462#M357935</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-12-06T15:00:35Z</dc:date>
    </item>
    <item>
      <title>Re: Use of output from proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-output-from-proc-freq/m-p/908130#M358416</link>
      <description>&lt;P&gt;Hi, I was presented with another scenario of data.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data output;
input time group var1 count percentage;
datalines;
0	1	1	3	42.8
0	1	2	4	57.14
0	2	1	5	45.45
0	2	2	5	45.45
0	2	3	1	9.09
3	1	1	2	28.6
3	1	2	5	71.5
3	2	1	1	50
3	2	2	1	50
6	1	2	3	100
6	2	1	2	100
9	1	2	2	100
9	2	1	2	66.6
9	2	2	2	33.3
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The difference is that some datasets will have data for var1=3 (see above). How can you add the counts for the same var1=3? Right now if I run the code as it is I get data want as this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data out;
input time group var1 count percentage;
datalines;
0	1	1	3	25
0	1	2	4	33.33333333
0	1	3	5	41.66666667
0	2	1	5	41.66666667
0	2	2	5	41.66666667
0	2	3	1	8.333333333
0	2	3	1	8.333333333
3	1	1	2	16.66666667
3	1	2	5	41.66666667
3	1	3	5	41.66666667
3	2	1	1	8.333333333
3	2	2	1	8.333333333
3	2	3	10	83.33333333
6	1	2	3	25
6	1	3	9	75
6	2	1	2	16.66666667
6	2	3	10	83.33333333
9	1	2	2	16.66666667
9	1	3	10	83.33333333
9	2	1	2	16.66666667
9	2	2	2	16.66666667
9	2	3	8	66.66666667
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But what I need is this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want;
input time group var1 count percentage;
datalines;
0	1	1	3	25
0	1	2	4	33.33333333
0	1	3	5	41.66666667
0	2	1	5	41.66666667
0	2	2	5	41.66666667
0	2	3	2	16.66666667
3	1	1	2	16.66666667
3	1	2	5	41.66666667
3	1	3	5	41.66666667
3	2	1	1	8.333333333
3	2	2	1	8.333333333
3	2	3	10	83.33333333
6	1	2	3	25
6	1	3	9	75
6	2	1	2	16.66666667
6	2	3	10	83.33333333
9	1	2	2	16.66666667
9	1	3	10	83.33333333
9	2	1	2	16.66666667
9	2	2	2	16.66666667
9	2	3	8	66.66666667
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Can the code provided be modified? Or how can data want be accomplished?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 00:27:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-output-from-proc-freq/m-p/908130#M358416</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-12-15T00:27:30Z</dc:date>
    </item>
    <item>
      <title>Re: Use of output from proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-output-from-proc-freq/m-p/908143#M358421</link>
      <description>&lt;P&gt;Assuming the data are sorted by TIME/GROUP/VAR1, then if you already have a VAR1=3 frequency, it will be the observation in which last.group=1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In such cases you don't want to do the extra OUTPUT statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, in the DO group that checks for last.group=1, just change that IF test from&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IF last.group then do:&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to a test that only works when last.group=1 and var1 is not already equal to 3.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 03:27:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-output-from-proc-freq/m-p/908143#M358421</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-12-15T03:27:57Z</dc:date>
    </item>
    <item>
      <title>Re: Use of output from proc freq</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-output-from-proc-freq/m-p/908886#M358591</link>
      <description>&lt;P&gt;Hi! Sorry for not getting back earlier. I deleted the second output, but the issue is that it only reports the 3's that were originally in the dataset and not the 3's derived from counting the observations from 1 and 2.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Dec 2023 19:39:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-output-from-proc-freq/m-p/908886#M358591</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2023-12-19T19:39:15Z</dc:date>
    </item>
  </channel>
</rss>

