<?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 - Using a calculated column in WHERE in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Using-a-calculated-column-in-WHERE/m-p/74492#M21627</link>
    <description>&lt;P&gt;Could you just use the suggestion SAS provided in your log, namely use having rather than where?&lt;BR /&gt;&lt;BR /&gt;Of course, I, personally, wouldn't try to create temp2 from temp2 thus in my example, below, create temp3:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp2;
set sashelp.class (rename=(
age=bank_id
height=tran_dt
weight=tran_am
));
run;
proc sql;
create table TEMP3 as 
select
bank_id /* bank id */
, MAX(tran_dt) as max_tran_dt /* latest transaction date */
, MIN(tran_dt) as min_tran_dt /* earliest transaction date */
, SUM(tran_am) as sum_tran_am /* total transaction amount */

from TEMP2
group by bank_id
having tran_dt between CALCULATED min_tran_dt
and CALCULATED max_tran_dt
order by bank_id
;
quit; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;HTH,&lt;BR /&gt;Art&lt;/P&gt;</description>
    <pubDate>Wed, 17 Jul 2019 18:59:46 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2019-07-17T18:59:46Z</dc:date>
    <item>
      <title>PROC SQL - Using a calculated column in WHERE</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Using-a-calculated-column-in-WHERE/m-p/74491#M21626</link>
      <description>I am trying to use calculated columns in my WHERE statement. From what I researched online, the below should work but I keep getting this error: &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;ERROR: Summary functions are restricted to the SELECT and HAVING clauses only.&lt;BR /&gt;
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.&lt;/B&gt;&lt;BR /&gt;
&lt;BR /&gt;
Below is my code. Did I code wrong? I want to aggregate the transaction amount for each bank between the two dates. Thank you for your suggestions!&lt;BR /&gt;
========================================================&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create table TEMP2 as 	&lt;BR /&gt;
select&lt;BR /&gt;
	bank_id /* bank id */&lt;BR /&gt;
	, MAX(tran_dt) as max_tran_dt /* latest transaction date */&lt;BR /&gt;
	, MIN(tran_dt) as min_tran_dt /* earliest transaction date */&lt;BR /&gt;
	, SUM(tran_am) as sum_tran_am /* total transaction amount */&lt;BR /&gt;
&lt;BR /&gt;
	from TEMP2&lt;BR /&gt;
	where tran_dt between CALCULATED min_tran_dt and CALCULATED max_tran_dt &lt;BR /&gt;
	group by bank_id&lt;BR /&gt;
	order by bank_id;&lt;BR /&gt;
quit;</description>
      <pubDate>Fri, 24 Sep 2010 14:47:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Using-a-calculated-column-in-WHERE/m-p/74491#M21626</guid>
      <dc:creator>SAS_user_n</dc:creator>
      <dc:date>2010-09-24T14:47:33Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - Using a calculated column in WHERE</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Using-a-calculated-column-in-WHERE/m-p/74492#M21627</link>
      <description>&lt;P&gt;Could you just use the suggestion SAS provided in your log, namely use having rather than where?&lt;BR /&gt;&lt;BR /&gt;Of course, I, personally, wouldn't try to create temp2 from temp2 thus in my example, below, create temp3:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp2;
set sashelp.class (rename=(
age=bank_id
height=tran_dt
weight=tran_am
));
run;
proc sql;
create table TEMP3 as 
select
bank_id /* bank id */
, MAX(tran_dt) as max_tran_dt /* latest transaction date */
, MIN(tran_dt) as min_tran_dt /* earliest transaction date */
, SUM(tran_am) as sum_tran_am /* total transaction amount */

from TEMP2
group by bank_id
having tran_dt between CALCULATED min_tran_dt
and CALCULATED max_tran_dt
order by bank_id
;
quit; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;HTH,&lt;BR /&gt;Art&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jul 2019 18:59:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Using-a-calculated-column-in-WHERE/m-p/74492#M21627</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2019-07-17T18:59:46Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - Using a calculated column in WHERE</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Using-a-calculated-column-in-WHERE/m-p/74493#M21628</link>
      <description>@SAS_user_n: You are trying to do something quite odd -- It seems that you are calculating the range of transaction dates just to select transactions whose date is between the two. Which is an unusually round about way of calculating the amount sub-totals by id.&lt;BR /&gt;
&lt;BR /&gt;
The only possible reason why you may be doing this is to exclude amounts whose date is missing. If this is the case, then I would do this directly as below. Better yet, you can use proc means/summary. Hope this helps.&lt;BR /&gt;
[pre]&lt;BR /&gt;
    data one;&lt;BR /&gt;
     input id tx am;&lt;BR /&gt;
   cards;&lt;BR /&gt;
   1 1 10&lt;BR /&gt;
   1 2 20&lt;BR /&gt;
   1 . 30&lt;BR /&gt;
   2 3 40&lt;BR /&gt;
   2 4 50&lt;BR /&gt;
   2 5 60&lt;BR /&gt;
   ;&lt;BR /&gt;
   run;&lt;BR /&gt;
&lt;BR /&gt;
   /* proc sql */&lt;BR /&gt;
   proc sql;&lt;BR /&gt;
     select id, sum(am) as totalAm&lt;BR /&gt;
     from one&lt;BR /&gt;
     where not missing(tx)&lt;BR /&gt;
     group by id&lt;BR /&gt;
     order by id;&lt;BR /&gt;
   quit;&lt;BR /&gt;
   /*&lt;BR /&gt;
         id   totalAm&lt;BR /&gt;
   ------------------&lt;BR /&gt;
          1        30&lt;BR /&gt;
          2       150&lt;BR /&gt;
   */&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
   /* get sum of amounts by id */&lt;BR /&gt;
   proc sort data=one;&lt;BR /&gt;
     by id;&lt;BR /&gt;
   run;&lt;BR /&gt;
   proc summary data=one; &lt;BR /&gt;
     var am;&lt;BR /&gt;
     by id;&lt;BR /&gt;
     output out=stats(drop=_:) sum(am)=totalAm;&lt;BR /&gt;
     where not missing(tx);&lt;BR /&gt;
   run;&lt;BR /&gt;
   /* check */&lt;BR /&gt;
   proc print data=stats noobs;&lt;BR /&gt;
   run;&lt;BR /&gt;
   /* on lst&lt;BR /&gt;
         total&lt;BR /&gt;
   id      Am&lt;BR /&gt;
    1      30&lt;BR /&gt;
    2     150&lt;BR /&gt;
   */&lt;BR /&gt;
[/pre]</description>
      <pubDate>Fri, 24 Sep 2010 21:12:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-Using-a-calculated-column-in-WHERE/m-p/74493#M21628</guid>
      <dc:creator>chang_y_chung_hotmail_com</dc:creator>
      <dc:date>2010-09-24T21:12:53Z</dc:date>
    </item>
  </channel>
</rss>

