<?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: Proc Sql WHERE clause for only one variable while calculating other variables without condition in SAS Data Science</title>
    <link>https://communities.sas.com/t5/SAS-Data-Science/Proc-Sql-WHERE-clause-for-only-one-variable-while-calculating/m-p/892442#M10574</link>
    <description>&lt;P&gt;Thank you so much! I would like to count the distinct number of VARIABLE_2 (not the total observation) where VARIABLE_3=1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the Proc Sql will give me the value 3 for VAR2_CNT (the VARIABLE_2 where variable3=1, 100,200,300) not 5 (total observation where variable3=1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;VARIABLE1&lt;/TD&gt;&lt;TD&gt;VARIABLE2&lt;/TD&gt;&lt;TD&gt;VARIABLE3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;19&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;40&lt;/TD&gt;&lt;TD&gt;400&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;500&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;16&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;19&lt;/TD&gt;&lt;TD&gt;400&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
    <pubDate>Sat, 02 Sep 2023 17:15:36 GMT</pubDate>
    <dc:creator>Lena_PA</dc:creator>
    <dc:date>2023-09-02T17:15:36Z</dc:date>
    <item>
      <title>Proc Sql WHERE clause for only one variable while calculating other variables without condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Proc-Sql-WHERE-clause-for-only-one-variable-while-calculating/m-p/892438#M10571</link>
      <description>&lt;P&gt;I'm trying to create a&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Proc Sql ;&lt;BR /&gt;create table NEW as select *, sum (VARIABLE_1) as VAR1_SUM, &amp;nbsp;count (distinct VARIABLE_2 where VARIABLE3=1) as VAR2_CNT&lt;BR /&gt;from HAVE group by week, store;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my current code and would not run.. Any suggestion?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Sep 2023 16:40:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Proc-Sql-WHERE-clause-for-only-one-variable-while-calculating/m-p/892438#M10571</guid>
      <dc:creator>Lena_PA</dc:creator>
      <dc:date>2023-09-02T16:40:45Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql WHERE clause for only one variable while calculating other variables without condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Proc-Sql-WHERE-clause-for-only-one-variable-while-calculating/m-p/892439#M10572</link>
      <description>&lt;P&gt;You probably want to use a CASE clause.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;count(case when (VARIABLE3=1) then VARIABLE_2 end ) as VAR2_CNT&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which will count how many observations have VARIABLE3=1 and VARIABLE_2 not missing.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Sep 2023 17:00:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Proc-Sql-WHERE-clause-for-only-one-variable-while-calculating/m-p/892439#M10572</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-02T17:00:26Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql WHERE clause for only one variable while calculating other variables without condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Proc-Sql-WHERE-clause-for-only-one-variable-while-calculating/m-p/892440#M10573</link>
      <description>&lt;P&gt;Show the LOG with the code and all messages, notes, warning or errors. Copy the text from the log, on&amp;nbsp; the forum open a text box using the &amp;lt;/&amp;gt; icon that appears above the message window and paste the text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The text box is important because it will preserve the formatting of the text and diagnostic characters. The main message windows will reformat text and appearance in the message window is hard to tell where any of the diagnostics actually appeared.&lt;/P&gt;</description>
      <pubDate>Sat, 02 Sep 2023 17:00:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Proc-Sql-WHERE-clause-for-only-one-variable-while-calculating/m-p/892440#M10573</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-09-02T17:00:06Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql WHERE clause for only one variable while calculating other variables without condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Proc-Sql-WHERE-clause-for-only-one-variable-while-calculating/m-p/892442#M10574</link>
      <description>&lt;P&gt;Thank you so much! I would like to count the distinct number of VARIABLE_2 (not the total observation) where VARIABLE_3=1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the Proc Sql will give me the value 3 for VAR2_CNT (the VARIABLE_2 where variable3=1, 100,200,300) not 5 (total observation where variable3=1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;VARIABLE1&lt;/TD&gt;&lt;TD&gt;VARIABLE2&lt;/TD&gt;&lt;TD&gt;VARIABLE3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;19&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;40&lt;/TD&gt;&lt;TD&gt;400&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;500&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;16&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;300&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;19&lt;/TD&gt;&lt;TD&gt;400&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Sat, 02 Sep 2023 17:15:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Proc-Sql-WHERE-clause-for-only-one-variable-while-calculating/m-p/892442#M10574</guid>
      <dc:creator>Lena_PA</dc:creator>
      <dc:date>2023-09-02T17:15:36Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql WHERE clause for only one variable while calculating other variables without condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Proc-Sql-WHERE-clause-for-only-one-variable-while-calculating/m-p/892447#M10575</link>
      <description>&lt;P&gt;If you want the count of the distinct values then just tell SQL to do that instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;count(distinct case when (VARIABLE3=1) then VARIABLE_2 end ) as VAR2_CNT&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Sep 2023 17:57:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Proc-Sql-WHERE-clause-for-only-one-variable-while-calculating/m-p/892447#M10575</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-02T17:57:37Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql WHERE clause for only one variable while calculating other variables without condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Proc-Sql-WHERE-clause-for-only-one-variable-while-calculating/m-p/892448#M10576</link>
      <description>&lt;P&gt;It really help to start with posting your input data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input var1-var3;
cards;
15 100 1
19 200 1
20 300 1
40 400 0
15 500 0
16 100 1
0 200 1
0 300 0
19 400 0
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And your expected output data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  input sum_var1 cnt_var2;
cards;
144 3
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That way others can provide tested code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select 
    sum(var1) as sum_var1
  , count(distinct case when(var3=1) then var2 end) as cnt_var2
  from have
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Sep 2023 18:04:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Proc-Sql-WHERE-clause-for-only-one-variable-while-calculating/m-p/892448#M10576</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-02T18:04:42Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql WHERE clause for only one variable while calculating other variables without condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Proc-Sql-WHERE-clause-for-only-one-variable-while-calculating/m-p/892454#M10577</link>
      <description>&lt;P&gt;Thank you so much! I will make sure to include the input data to begin with next time!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Sep 2023 19:02:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Proc-Sql-WHERE-clause-for-only-one-variable-while-calculating/m-p/892454#M10577</guid>
      <dc:creator>Lena_PA</dc:creator>
      <dc:date>2023-09-02T19:02:02Z</dc:date>
    </item>
  </channel>
</rss>

