<?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: Add missing values when counting with proc sql in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Add-missing-values-when-counting-with-proc-sql/m-p/364972#M64704</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Test Grades$;
cards;
1      A
1      B
1      B
1      A
1      C
1      B
1      A
1      C
1      B
1      A
;

data x;
input Grades $;
cards;
A
B
C
D
;
run;
data temp;
 set have(in=ina) x;
 w=ina;
run;
proc freq data=temp noprint;
table test*Grades/list out=want(where=(test is not missing)) ;
weight w/zeros;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 07 Jun 2017 13:31:52 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2017-06-07T13:31:52Z</dc:date>
    <item>
      <title>Add missing values when counting with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Add-missing-values-when-counting-with-proc-sql/m-p/364859#M64695</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've got a data set that looks like this (let's assume the grades are only A B C D):&lt;/P&gt;&lt;P&gt;Test Grades&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;C&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;C&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to count how many grades are there for each type (including D) and the result to look like the following:&lt;/P&gt;&lt;P&gt;Test Grades Number&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; C &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; D &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using the followin code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select test, grades, count(*) as number&lt;BR /&gt;from have&lt;BR /&gt;group by &lt;SPAN&gt;est&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;grades&lt;/SPAN&gt;&lt;BR /&gt;order by &lt;SPAN&gt;est&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;grades&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And the problem is that this code doesn't add the row that shows there are 0 D.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 08:37:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Add-missing-values-when-counting-with-proc-sql/m-p/364859#M64695</guid>
      <dc:creator>wankata5</dc:creator>
      <dc:date>2017-06-07T08:37:06Z</dc:date>
    </item>
    <item>
      <title>Re: Add missing values when counting with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Add-missing-values-when-counting-with-proc-sql/m-p/364861#M64696</link>
      <description>&lt;P&gt;Yes, it will only count data which is present in the dataset, how can the procedure know that D should be there?&lt;/P&gt;
&lt;P&gt;Do your calculation, then merge on a list of exepected values (to get working code post test data in the form of a datastep):&lt;/P&gt;
&lt;PRE&gt;data default_vals;
  input grades $;
  test=1;
datalines;
A
B
C
D
;
run;

/* Your proc sql */

data want;
  merge want default_vals;
  by test grades;
  if number=. then number=0;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 08:56:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Add-missing-values-when-counting-with-proc-sql/m-p/364861#M64696</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-06-07T08:56:29Z</dc:date>
    </item>
    <item>
      <title>Re: Add missing values when counting with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Add-missing-values-when-counting-with-proc-sql/m-p/364862#M64697</link>
      <description>&lt;P&gt;In your data there are no "D" nor infinit number of other values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As much as I know you cannot do what you want by sql.&lt;/P&gt;
&lt;P&gt;You can do it by data step, deffining array of all expected codes to count intiated as 0,&lt;/P&gt;
&lt;P&gt;At end of input you can print or output the counters. Some of them may contain zero.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have end=eof
       retain cnt c1-c5 0;
       ix = index('ABCD',grade);
       if ix le 5 then cnt(ix) +1;
       if eof then do;
          do ix=1 to dim(cnt);
              grade = substr('ABCD',ix,1);
              count = cnt(ix);
              output;
          end; 
       end;             
       keep grade count;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Jun 2017 08:57:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Add-missing-values-when-counting-with-proc-sql/m-p/364862#M64697</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-06-07T08:57:03Z</dc:date>
    </item>
    <item>
      <title>Re: Add missing values when counting with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Add-missing-values-when-counting-with-proc-sql/m-p/364863#M64698</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Test Grades$;
&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;var=1;&lt;/STRONG&gt;&lt;/FONT&gt;
cards;
1      A
1      B
1      B
1      A
1      C
1      B
1      A
1      C
1      B
1      A
;

proc format fmtlib;
value $grd
'A'='A'
'B'='B'
'C'='C'
'D'='D'
;
run;

proc summary data=have completetypes nway;
class test;
class  grades / preloadfmt;
var var;
format grades $grd.;
output out=want n=number;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Jun 2017 09:09:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Add-missing-values-when-counting-with-proc-sql/m-p/364863#M64698</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2017-06-07T09:09:41Z</dc:date>
    </item>
    <item>
      <title>Re: Add missing values when counting with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Add-missing-values-when-counting-with-proc-sql/m-p/364971#M64703</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Test Grades$;
var=1;
cards;
1      A
1      B
1      B
1      A
1      C
1      B
1      A
1      C
1      B
1      A
;

data x;
input x $;
cards;
A
B
C
D
;
run;

proc sql;
select a.test,a.x as grades,coalesce(sum(var),0) as count
 from (
 select * from
 (select distinct test from have),
 (select distinct x from x)
 ) as a left join have as b 
  on a.test=b.test and a.x=b.grades 
  group by a.test,a.x;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Jun 2017 13:27:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Add-missing-values-when-counting-with-proc-sql/m-p/364971#M64703</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-06-07T13:27:24Z</dc:date>
    </item>
    <item>
      <title>Re: Add missing values when counting with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Add-missing-values-when-counting-with-proc-sql/m-p/364972#M64704</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Test Grades$;
cards;
1      A
1      B
1      B
1      A
1      C
1      B
1      A
1      C
1      B
1      A
;

data x;
input Grades $;
cards;
A
B
C
D
;
run;
data temp;
 set have(in=ina) x;
 w=ina;
run;
proc freq data=temp noprint;
table test*Grades/list out=want(where=(test is not missing)) ;
weight w/zeros;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Jun 2017 13:31:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Add-missing-values-when-counting-with-proc-sql/m-p/364972#M64704</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-06-07T13:31:52Z</dc:date>
    </item>
  </channel>
</rss>

