<?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: How to group by condition using SQL? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-condition-using-SQL/m-p/441455#M110449</link>
    <description>&lt;P&gt;You can force both variables to exist with an extra step such as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
input
YEAR SALE ID $5.;
datalines;
2001  12  ab34
2001  10  1234
2001  14  ab34
2001  10  2234
2002  10  1234
2002  10  ab34
2002  10  2234
;

data both;
length year fyear 8;
set have;
run;

proc sql;
create table want as
select
coalesce(YEAR, FYEAR) as year,
SUM(SALE) as TOTALSALE
from both
group by calculated year;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 02 Mar 2018 04:12:14 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2018-03-02T04:12:14Z</dc:date>
    <item>
      <title>How to group by condition using SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-condition-using-SQL/m-p/441402#M110425</link>
      <description>&lt;P&gt;I am trying to capture a certain variable according to certain variable.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
input
YEAR SALE ID $5.;
datalines;
2001  12  ab34
2001  10  1234
2001  14  ab34
2001  10  2234
2002  10  1234
2002  10  ab34
2002  10  2234;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;Some dataset has variable YEAR, but some has FYEAR, how can I group by YEAR, and group by FYEAR when YEAR is missing?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Can they be written in the GROUP BY code?&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
SUM(SALE) as TOTALSALE
from have
group by YEAR or FYEAR; /*How can I realize this step using code? SQL did not recgonize OR*/
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;Thank you very much!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 22:13:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-condition-using-SQL/m-p/441402#M110425</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2018-03-01T22:13:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to group by condition using SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-condition-using-SQL/m-p/441403#M110426</link>
      <description>&lt;P&gt;If the variable doesn't exist that's problematic.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would suggest using a macro to check if it exists and if it does uses the correct variable. Or rename them all to be standardized.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 22:15:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-condition-using-SQL/m-p/441403#M110426</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-01T22:15:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to group by condition using SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-condition-using-SQL/m-p/441407#M110428</link>
      <description>&lt;P&gt;If both variables exist but one value is missing, use&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;SPAN class="token keyword"&gt;group&lt;/SPAN&gt; &lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; coalesce(&lt;SPAN class="token function"&gt;YEAR,&lt;/SPAN&gt; FYEAR)&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 22:29:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-condition-using-SQL/m-p/441407#M110428</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-03-01T22:29:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to group by condition using SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-condition-using-SQL/m-p/441423#M110435</link>
      <description>&lt;P&gt;No they are both existing. Just some datasets have YEAR only, some datasets have FYEAR only.&lt;/P&gt;&lt;P&gt;Can I still use this code?&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 23:34:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-condition-using-SQL/m-p/441423#M110435</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2018-03-01T23:34:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to group by condition using SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-condition-using-SQL/m-p/441450#M110445</link>
      <description>&lt;P&gt;Yes, precicely.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Mar 2018 03:29:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-condition-using-SQL/m-p/441450#M110445</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-03-02T03:29:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to group by condition using SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-condition-using-SQL/m-p/441451#M110446</link>
      <description>&lt;P&gt;No, if the variable doesn't exist it won't work.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 69         proc sql;
 70         create table want as
 71         select *
 72         from sashelp.class
 73         order by coalesce(age, new_age);
 ERROR: The COALESCE function requires its arguments to be of the same data type.
 ERROR: The following columns were not found in the contributing tables: new_age.
 NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
 74         quit;
 NOTE: The SAS System stopped processing this step because of errors.
 NOTE: PROCEDURE SQL used (Total process time):
       real time           0.00 seconds
       cpu time            0.00 seconds&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Mar 2018 03:44:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-condition-using-SQL/m-p/441451#M110446</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-02T03:44:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to group by condition using SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-condition-using-SQL/m-p/441452#M110447</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/188461"&gt;@yanshuai&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;No they are both existing. &lt;STRONG&gt;Just some datasets have YEAR only, some datasets have FYEAR only.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Can I still use this code?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;is asking if they both exist in the same data set, you answer seems to indicate that it doesn't, therefore coalesce will not work.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Mar 2018 03:46:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-condition-using-SQL/m-p/441452#M110447</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-02T03:46:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to group by condition using SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-condition-using-SQL/m-p/441455#M110449</link>
      <description>&lt;P&gt;You can force both variables to exist with an extra step such as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
input
YEAR SALE ID $5.;
datalines;
2001  12  ab34
2001  10  1234
2001  14  ab34
2001  10  2234
2002  10  1234
2002  10  ab34
2002  10  2234
;

data both;
length year fyear 8;
set have;
run;

proc sql;
create table want as
select
coalesce(YEAR, FYEAR) as year,
SUM(SALE) as TOTALSALE
from both
group by calculated year;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Mar 2018 04:12:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-group-by-condition-using-SQL/m-p/441455#M110449</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-03-02T04:12:14Z</dc:date>
    </item>
  </channel>
</rss>

