<?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 PROC SQL fill in missing class variable, similar to preloadfmt function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-fill-in-missing-class-variable-similar-to-preloadfmt/m-p/896553#M354257</link>
    <description>&lt;P&gt;Hello, is there a way for me to fill in the missing category of my class variable with 0 in PROC SQL? I know PROC REPORT can do that, but it seems i can't find ways to work around it in PROC SQL.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My code looks like below whereas eth includes 7 different groups, but for this one, only 6 of them have nonmissing results, therefore i am only seeing 6 groups after SAS ran PROC SQL. But I want to still have the 7th group but plug in 0s across all measures.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;is that doable? Thanks!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	CREATE TABLE TBL26 AS
	SELECT eth, 
				COUNT(DISTINCT CASE WHEN(AMT=0) THEN UCI END) AS nopos,
				COUNT(DISTINCT CASE WHEN(AMT&amp;gt;0) THEN UCI END) AS yespos,
				COUNT(DISTINCT UCI) AS TOTCNT,
				CALCULATED NOPOS/CALCULATED TOTCNT AS SHARE
			FROM &amp;amp;Regctr.(WHERE=(AGE='00-02')) 
			GROUP BY eth
		UNION ALL
		SELECT 'Z',
			COUNT(DISTINCT UCI) AS TOTCNT,
			COUNT(DISTINCT CASE WHEN(AMT=0) THEN UCI END) AS NOPOS,
			COUNT(DISTINCT CASE WHEN(AMT&amp;gt;0) THEN UCI END) AS YESPOS,
			CALCULATED NOPOS/CALCULATED TOTCNT AS SHARE
		FROM &amp;amp;Regctr.(WHERE=(AGE='00-02'))
		

; QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 30 Sep 2023 06:29:43 GMT</pubDate>
    <dc:creator>kevsma</dc:creator>
    <dc:date>2023-09-30T06:29:43Z</dc:date>
    <item>
      <title>PROC SQL fill in missing class variable, similar to preloadfmt function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-fill-in-missing-class-variable-similar-to-preloadfmt/m-p/896553#M354257</link>
      <description>&lt;P&gt;Hello, is there a way for me to fill in the missing category of my class variable with 0 in PROC SQL? I know PROC REPORT can do that, but it seems i can't find ways to work around it in PROC SQL.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My code looks like below whereas eth includes 7 different groups, but for this one, only 6 of them have nonmissing results, therefore i am only seeing 6 groups after SAS ran PROC SQL. But I want to still have the 7th group but plug in 0s across all measures.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;is that doable? Thanks!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	CREATE TABLE TBL26 AS
	SELECT eth, 
				COUNT(DISTINCT CASE WHEN(AMT=0) THEN UCI END) AS nopos,
				COUNT(DISTINCT CASE WHEN(AMT&amp;gt;0) THEN UCI END) AS yespos,
				COUNT(DISTINCT UCI) AS TOTCNT,
				CALCULATED NOPOS/CALCULATED TOTCNT AS SHARE
			FROM &amp;amp;Regctr.(WHERE=(AGE='00-02')) 
			GROUP BY eth
		UNION ALL
		SELECT 'Z',
			COUNT(DISTINCT UCI) AS TOTCNT,
			COUNT(DISTINCT CASE WHEN(AMT=0) THEN UCI END) AS NOPOS,
			COUNT(DISTINCT CASE WHEN(AMT&amp;gt;0) THEN UCI END) AS YESPOS,
			CALCULATED NOPOS/CALCULATED TOTCNT AS SHARE
		FROM &amp;amp;Regctr.(WHERE=(AGE='00-02'))
		

; QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 30 Sep 2023 06:29:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-fill-in-missing-class-variable-similar-to-preloadfmt/m-p/896553#M354257</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2023-09-30T06:29:43Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL fill in missing class variable, similar to preloadfmt function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-fill-in-missing-class-variable-similar-to-preloadfmt/m-p/896555#M354258</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/379997"&gt;@kevsma&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can create a small auxiliary dataset which just contains all values of ETH. If all these values occur in the unrestricted dataset &lt;FONT face="courier new,courier"&gt;&amp;amp;Regctr.&lt;/FONT&gt;, the preliminary step could look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table alleth as
select distinct eth from &amp;amp;Regctr.;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Otherwise &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/sqlproc/n00p1g1wr4q7pzn1e6o5i82n7iml.htm" target="_blank" rel="noopener"&gt;INSERT&lt;/A&gt; additional ETH values into this dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then use the complete ALLETH dataset, e.g., in a RIGHT JOIN:&lt;/P&gt;
&lt;PRE&gt;proc sql;
create table tbl26 as
select &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;a.&lt;/STRONG&gt;&lt;/FONT&gt;eth, 
       count(distinct case when(amt=0) then uci end) as nopos,
       count(distinct case when(amt&amp;gt;0) then uci end) as yespos,
       count(distinct uci) as totcnt,
       &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;coalesce(&lt;/STRONG&gt;&lt;/FONT&gt;calculated nopos/calculated totcnt&lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;,0)&lt;/FONT&gt;&lt;/STRONG&gt; as share
   from &amp;amp;regctr.(where=(age='00-02'))&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt; natural right join alleth a&lt;/STRONG&gt;&lt;/FONT&gt;
   group by &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;a.&lt;/STRONG&gt;&lt;/FONT&gt;eth
union all
select 'z',
       count(distinct case when(amt=0) then uci end) as nopos,
       count(distinct case when(amt&amp;gt;0) then uci end) as yespos,
       &lt;STRONG&gt;count(distinct uci) as totcnt,&lt;/STRONG&gt;
       calculated nopos/calculated totcnt as share
   from &amp;amp;regctr.(where=(age='00-02'));
quit;&lt;/PRE&gt;
&lt;P&gt;The COALESCE function can be omitted if you accept SHARE=. for the 0/0 case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that I have corrected a mistake in your second SELECT statement: Without the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/sqlproc/p1o6k7t8y56hobn1mup90vpf4ye6.htm#n1fc9hz8hox0gun13fxkyv5hmn54" target="_blank" rel="noopener"&gt;CORR keyword&lt;/A&gt;&amp;nbsp;of the UNION operator the variables would be matched by ordinal position, so with your code TOTCNT from the second SELECT would be matched with NOPOS from the first SELECT, NOPOS with YESPOS and so on! Alternatively (more robust), use the CORR keyword:&lt;/P&gt;
&lt;PRE&gt;union all &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;corr&lt;/STRONG&gt;&lt;/FONT&gt;
select 'z' &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;as eth&lt;/STRONG&gt;&lt;/FONT&gt;,&lt;/PRE&gt;</description>
      <pubDate>Sat, 30 Sep 2023 10:09:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-fill-in-missing-class-variable-similar-to-preloadfmt/m-p/896555#M354258</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-09-30T10:09:18Z</dc:date>
    </item>
  </channel>
</rss>

