<?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: 'Where' statement apply to specific column when creating a table with PROC SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Where-statement-apply-to-specific-column-when-creating-a-table/m-p/762278#M241325</link>
    <description>&lt;P&gt;I cant' make heads or tails out of your posted code.&amp;nbsp; So let's just look at your question instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;I would like to do a 'where' statement within a mathematic SQL function. That is, I want to have the total number of patients in one column (N_SUBJ) and the total number of patients that meet a specific criteria to be a different column (N_DEV).&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So get a count in SQL use the COUNT() aggregate function.&amp;nbsp; It will ignore (count as zero) missing values.&amp;nbsp; So just use a CASE clause that returns a missing value for the observation you don't want to count.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select count(*) as n_students
     , count(case when sex='F' then 1 else . end) as n_female
     , count(case when sex='M' then 1 else . end) as n_male
from sashelp.class
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In SAS you can also use the fact that boolean expressions evaluate as 1 for TRUE and 0 for FALSE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select count(*) as n_students
     , sum(sex='F') as n_female
     , sum(sex='M') as n_male
from sashelp.class
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 18 Aug 2021 13:31:57 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-08-18T13:31:57Z</dc:date>
    <item>
      <title>'Where' statement apply to specific column when creating a table with PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-statement-apply-to-specific-column-when-creating-a-table/m-p/762163#M241268</link>
      <description>&lt;P&gt;I would like to do a 'where' statement within a mathematic SQL function. That is, I want to have the total number of patients in one column (N_SUBJ) and the total number of patients that meet a specific criteria to be a different column (N_DEV).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Code:&lt;/STRONG&gt; I've been messing with the first line to see what will get rid of the initial error.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;&lt;BR /&gt;create table CohortA as&lt;BR /&gt;/* Eligibility/enrollment */&lt;BR /&gt;select '_1Elig' as CAT, '_01AnyElig' as DEVTYPE, count(distinct PATID) as N_SUBJ, (count(distinct PATID from dv4 where DEV3DECOD in ('03', '04', '06', '99'))) as N_DEV&lt;BR /&gt;from dv4 where DVCAT = '01' and ARMCD in ('A', 'A_AND_B')&lt;BR /&gt;union &lt;BR /&gt;select '_1Elig' as CAT, '_02NoIncl' as DEVTYPE, count(distinct PATID) as N_SUBJ, count(distinct PATID) where DEV3DECOD = '03' as N_DEV&lt;BR /&gt;from dv4 where DVCAT = '01' and ARMCD in ('A', 'A_AND_B')&lt;BR /&gt;select '_1Elig' as CAT, '_03Excl' as DEVTYPE, count(distinct PATID) as N_SUBJ, count(distinct PATID) where DEV3DECOD = '04' as N_DEV&lt;BR /&gt;from dv4 where DVCAT = '01' and ARMCD in ('A', 'A_AND_B')&lt;BR /&gt;union &lt;BR /&gt;select '_1Elig' as CAT, '_04NoSign' as DEVTYPE, count(distinct PATID) as N_SUBJ, count(distinct PATID) where DEV3DECOD = '06' as N_DEV&lt;BR /&gt;from dv4 where DVCAT = '01' and ARMCD in ('A', 'A_AND_B') select '_1Elig' as CAT, '_01AnyElig' as DEVTYPE, count(distinct PATID) as N_SUBJ, count(distinct PATID) where DEV3DECOD in ('03', '04', '06', '99') &lt;BR /&gt;from dv4 where DVCAT = '01' and ARMCD in ('A', 'A_AND_B')&lt;BR /&gt;union &lt;BR /&gt;select '_1Elig' as CAT, '_05Oth' as DEVTYPE, count(distinct PATID) as N_SUBJ, count(distinct PATID) where DEV3DECOD = '99' as N_DEV&lt;BR /&gt;from dv4 where DVCAT = '01' and ARMCD in ('A', 'A_AND_B') select '_1Elig' as CAT, '_01AnyElig' as DEVTYPE, count(distinct PATID) as N_SUBJ, count(distinct PATID) where DEV3DECOD in ('03', '04', '06', '99') &lt;BR /&gt;from dv4 where DVCAT = '01' and ARMCD in ('A', 'A_AND_B');&lt;BR /&gt;quit;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;Table Desire:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mariko5797_0-1629231805539.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/62672i8F8559831DEB2757/image-size/medium?v=v2&amp;amp;px=400" role="button" title="mariko5797_0-1629231805539.png" alt="mariko5797_0-1629231805539.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Thank you in advance.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Aug 2021 20:25:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-statement-apply-to-specific-column-when-creating-a-table/m-p/762163#M241268</guid>
      <dc:creator>mariko5797</dc:creator>
      <dc:date>2021-08-17T20:25:09Z</dc:date>
    </item>
    <item>
      <title>Re: 'Where' statement apply to specific column when creating a table with PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-statement-apply-to-specific-column-when-creating-a-table/m-p/762165#M241269</link>
      <description>&lt;P&gt;*Note: I just realized some other parts of my code are wrong. Please ignore them for the sake of the question posed. Thank you&lt;/P&gt;</description>
      <pubDate>Tue, 17 Aug 2021 20:33:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-statement-apply-to-specific-column-when-creating-a-table/m-p/762165#M241269</guid>
      <dc:creator>mariko5797</dc:creator>
      <dc:date>2021-08-17T20:33:51Z</dc:date>
    </item>
    <item>
      <title>Re: 'Where' statement apply to specific column when creating a table with PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-statement-apply-to-specific-column-when-creating-a-table/m-p/762173#M241273</link>
      <description>It looks like your code is roughly the same except for this:&lt;BR /&gt;DEV3DECOD &lt;BR /&gt;&lt;BR /&gt;Is that case?&lt;BR /&gt;&lt;BR /&gt;It also looks like you're trying to do totals and then subtotals in the same query? Have you tried some of more standard procs like MEANS or FREQ that do these out of the box? For the distinct count that may have to be by SQL or double PROC FREQ?</description>
      <pubDate>Tue, 17 Aug 2021 21:34:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-statement-apply-to-specific-column-when-creating-a-table/m-p/762173#M241273</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-08-17T21:34:58Z</dc:date>
    </item>
    <item>
      <title>Re: 'Where' statement apply to specific column when creating a table with PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-statement-apply-to-specific-column-when-creating-a-table/m-p/762228#M241311</link>
      <description>&lt;P&gt;Why does N_SUBJ change when there's no condition attached to it?&lt;/P&gt;</description>
      <pubDate>Wed, 18 Aug 2021 06:33:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-statement-apply-to-specific-column-when-creating-a-table/m-p/762228#M241311</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-08-18T06:33:09Z</dc:date>
    </item>
    <item>
      <title>Re: 'Where' statement apply to specific column when creating a table with PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-statement-apply-to-specific-column-when-creating-a-table/m-p/762278#M241325</link>
      <description>&lt;P&gt;I cant' make heads or tails out of your posted code.&amp;nbsp; So let's just look at your question instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;I would like to do a 'where' statement within a mathematic SQL function. That is, I want to have the total number of patients in one column (N_SUBJ) and the total number of patients that meet a specific criteria to be a different column (N_DEV).&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So get a count in SQL use the COUNT() aggregate function.&amp;nbsp; It will ignore (count as zero) missing values.&amp;nbsp; So just use a CASE clause that returns a missing value for the observation you don't want to count.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select count(*) as n_students
     , count(case when sex='F' then 1 else . end) as n_female
     , count(case when sex='M' then 1 else . end) as n_male
from sashelp.class
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In SAS you can also use the fact that boolean expressions evaluate as 1 for TRUE and 0 for FALSE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select count(*) as n_students
     , sum(sex='F') as n_female
     , sum(sex='M') as n_male
from sashelp.class
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Aug 2021 13:31:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-statement-apply-to-specific-column-when-creating-a-table/m-p/762278#M241325</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-08-18T13:31:57Z</dc:date>
    </item>
  </channel>
</rss>

