<?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: Percent not adding up in proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Percent-not-adding-up-in-proc-sql/m-p/925822#M364298</link>
    <description>&lt;P&gt;I would advise you to use PROC FREQ whenever you want to count things, instead of creating your own somewhat complicated SQL method, which clearly didn't work. SAS has put a huge amount of effort into validating the PROC FREQ results and tested it in a bazillion real world situations so you don't have to worry about getting the wrong output.&lt;/P&gt;</description>
    <pubDate>Thu, 25 Apr 2024 17:04:09 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2024-04-25T17:04:09Z</dc:date>
    <item>
      <title>Percent not adding up in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Percent-not-adding-up-in-proc-sql/m-p/925802#M364293</link>
      <description>&lt;P&gt;I have a long code, but made a mock one for our purposes here, which creates the same error. I have 5 out of 10 patients that were evaluated, yet I get 60% instead of 50% evaluated when using "else 0". But when I use "else .", I get 50%, which is correct. I'm wondering why? Problem solved after 2 days, but perhaps someone can help me understand why using else 0 vs. else .&amp;nbsp; would make a difference?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Mock data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data test;
   input patient_id evaluated $;
   datalines;
1 Yes
2 Yes
3 Yes
4 Yes
5 Yes
6 No
7 No
8 No
9 No
10 No
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="test.PNG" style="width: 203px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/95875i63BC28849B7BB29B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="test.PNG" alt="test.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First code that gives me 60% with "else 0" in the second subquery:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table test1 as
select "Evaluation Complete" as Variable,

	(select (Count(distinct patient_id))
		from test
		where evaluated = "Yes") as Count,

	(select (Count(distinct case when evaluated="Yes" then patient_id &lt;STRONG&gt;else 0&lt;/STRONG&gt; end)/(Count(distinct patient_id)*1.0))
		from test) as Percent format percent8.1

from test(obs=1);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="test1.PNG" style="width: 343px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/95873iCCD3B434C58F1BAC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="test1.PNG" alt="test1.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second code where we replace "else 0" with "else ." and gives us 50% for patients evaluated (correct):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table test2 as
select "Evaluation Complete" as Variable,

	(select (Count(distinct patient_id))
		from test
		where evaluated = "Yes") as Count,

	(select (Count(distinct case when evaluated="Yes" then patient_id &lt;STRONG&gt;else .&lt;/STRONG&gt; end)/(Count(distinct patient_id)*1.0))
		from test) as Percent format percent8.1

from test(obs=1);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="test2.PNG" style="width: 338px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/95874iD10DA86F469B10B7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="test2.PNG" alt="test2.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 15:46:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Percent-not-adding-up-in-proc-sql/m-p/925802#M364293</guid>
      <dc:creator>vegan_renegade</dc:creator>
      <dc:date>2024-04-25T15:46:44Z</dc:date>
    </item>
    <item>
      <title>Re: Percent not adding up in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Percent-not-adding-up-in-proc-sql/m-p/925813#M364295</link>
      <description>&lt;P&gt;You're assigning the value of 0 to all ones you don't want to count, which is the extra value so you get 6 distinct values (5 actual IDs plus the 0). &lt;BR /&gt;&lt;BR /&gt;Missing is not counted so you get 5 values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table test1 as
select "Evaluation Complete" as Variable,

 *, case when evaluated="Yes" then patient_id else 0 end as variable0,
 case when evaluated="Yes" then patient_id else . end as variable_missing
from test;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Reeza_1-1714062916344.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/95877iA94421DD5F53F7F5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Reeza_1-1714062916344.png" alt="Reeza_1-1714062916344.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/301219"&gt;@vegan_renegade&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a long code, but made a mock one for our purposes here, which creates the same error. I have 5 out of 10 patients that were evaluated, yet I get 60% instead of 50% evaluated when using "else 0". But when I use "else .", I get 50%, which is correct. I'm wondering why? Problem solved after 2 days, but perhaps someone can help me understand why using else 0 vs. else .&amp;nbsp; would make a difference?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Mock data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data test;
   input patient_id evaluated $;
   datalines;
1 Yes
2 Yes
3 Yes
4 Yes
5 Yes
6 No
7 No
8 No
9 No
10 No
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="test.PNG" style="width: 203px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/95875i63BC28849B7BB29B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="test.PNG" alt="test.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First code that gives me 60% with "else 0" in the second subquery:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table test1 as
select "Evaluation Complete" as Variable,

	(select (Count(distinct patient_id))
		from test
		where evaluated = "Yes") as Count,

	(select (Count(distinct case when evaluated="Yes" then patient_id &lt;STRONG&gt;else 0&lt;/STRONG&gt; end)/(Count(distinct patient_id)*1.0))
		from test) as Percent format percent8.1

from test(obs=1);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="test1.PNG" style="width: 343px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/95873iCCD3B434C58F1BAC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="test1.PNG" alt="test1.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second code where we replace "else 0" with "else ." and gives us 50% for patients evaluated (correct):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table test2 as
select "Evaluation Complete" as Variable,

	(select (Count(distinct patient_id))
		from test
		where evaluated = "Yes") as Count,

	(select (Count(distinct case when evaluated="Yes" then patient_id &lt;STRONG&gt;else .&lt;/STRONG&gt; end)/(Count(distinct patient_id)*1.0))
		from test) as Percent format percent8.1

from test(obs=1);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="test2.PNG" style="width: 338px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/95874iD10DA86F469B10B7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="test2.PNG" alt="test2.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 16:35:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Percent-not-adding-up-in-proc-sql/m-p/925813#M364295</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2024-04-25T16:35:22Z</dc:date>
    </item>
    <item>
      <title>Re: Percent not adding up in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Percent-not-adding-up-in-proc-sql/m-p/925817#M364297</link>
      <description>&lt;P&gt;I think the issue is that COUNT will count any valid value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try:&lt;/P&gt;
&lt;PRE&gt;proc sql;
create table test1 as
select "Evaluation Complete" as Variable,

	(select (Count(distinct patient_id))
		from test
		where evaluated = "Yes") as Count,

	(select (Count(distinct case when evaluated="Yes" then patient_id else . end)/(Count(distinct patient_id)*1.0))
		from test) as Percent format percent8.1

from test(obs=1);
quit;&lt;/PRE&gt;
&lt;P&gt;Your example doesn't include any repeated patient_id. If your data doesn't have any such I would question why do this instead of Proc Freq.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 16:36:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Percent-not-adding-up-in-proc-sql/m-p/925817#M364297</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-04-25T16:36:07Z</dc:date>
    </item>
    <item>
      <title>Re: Percent not adding up in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Percent-not-adding-up-in-proc-sql/m-p/925822#M364298</link>
      <description>&lt;P&gt;I would advise you to use PROC FREQ whenever you want to count things, instead of creating your own somewhat complicated SQL method, which clearly didn't work. SAS has put a huge amount of effort into validating the PROC FREQ results and tested it in a bazillion real world situations so you don't have to worry about getting the wrong output.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2024 17:04:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Percent-not-adding-up-in-proc-sql/m-p/925822#M364298</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-04-25T17:04:09Z</dc:date>
    </item>
  </channel>
</rss>

