<?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: Max of variable based on condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Max-of-variable-based-on-condition/m-p/649451#M194716</link>
    <description>&lt;P&gt;Use this simpler syntax:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql; 
  select max(WEIGHT)*(AGE=12)
        ,max(WEIGHT)*(AGE=13)
  from SASHELP.CLASS
  group by AGE;
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 21 May 2020 05:01:56 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2020-05-21T05:01:56Z</dc:date>
    <item>
      <title>Max of variable based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-of-variable-based-on-condition/m-p/649434#M194706</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am hoping you can assist me without viewing my data. I am trying to get the max of a variable when a number is lower than a certain figure (the number represents difference in dates). I am getting an error that I cannot figure out tho:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql; 
create table data_4 as select customer_id, CLASSIFICATION, EXTR, RISK_SCORE, calendar_month, date,
max(date) as max_date format date9., intck('month', date, CALCULATED max_date) as date_diff, cc,

max(case when CALCULATED date_diff le 3 then cc else 0 end) as Low_cc_l3,
max(case when CALCULATED date_diff le 6 then cc else 0 end) as Low_cc_l6,
max(case when CALCULATED date_diff le 12 then cc else 0 end) as Low_cc_l12

from data_3 group by customer_id order by customer_id, calendar_month;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The error I am getting is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: Summary functions nested in this way are not supported.&lt;BR /&gt;ERROR: Summary functions nested in this way are not supported.&lt;BR /&gt;ERROR: Summary functions nested in this way are not supported.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to return the maximum CC in last 3, 6, 12 months.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is appreciated.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 02:00:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-of-variable-based-on-condition/m-p/649434#M194706</guid>
      <dc:creator>Scott86</dc:creator>
      <dc:date>2020-05-21T02:00:13Z</dc:date>
    </item>
    <item>
      <title>Re: Max of variable based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-of-variable-based-on-condition/m-p/649451#M194716</link>
      <description>&lt;P&gt;Use this simpler syntax:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql; 
  select max(WEIGHT)*(AGE=12)
        ,max(WEIGHT)*(AGE=13)
  from SASHELP.CLASS
  group by AGE;
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 05:01:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-of-variable-based-on-condition/m-p/649451#M194716</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-05-21T05:01:56Z</dc:date>
    </item>
    <item>
      <title>Re: Max of variable based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-of-variable-based-on-condition/m-p/649455#M194719</link>
      <description>&lt;P&gt;That worked thank you. I've never seen SQL SAS used that way. What are the * doing? Also to get last 3 months I had to change le 3 to le 2 and le 6 to le 5 and so on. Why is that?&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 05:32:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-of-variable-based-on-condition/m-p/649455#M194719</guid>
      <dc:creator>Scott86</dc:creator>
      <dc:date>2020-05-21T05:32:58Z</dc:date>
    </item>
    <item>
      <title>Re: Max of variable based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-of-variable-based-on-condition/m-p/649457#M194721</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Wouldn't a subquery be a solution?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data_3;
input customer_id calendar_month date yymmdd10. cc;
format date yymmdd10.;
cards;
1 1 2020-01-01 60
1 2 2020-02-01 50
1 3 2020-03-01 40
1 4 2020-04-01 30
1 5 2020-05-01 20
1 6 2020-06-01 10
2 1 2020-01-01 60
2 2 2020-02-01 50
2 3 2020-03-01 40
2 4 2020-04-01 30
2 5 2020-05-01 20
2 6 2020-06-01 10
;
run;



proc sql; 
create table data_4 as 
select a.*,

max(case when a.date_diff le 3 then a.cc else 0 end) as Low_cc_l3,
max(case when a.date_diff le 6 then a.cc else 0 end) as Low_cc_l6,
max(case when a.date_diff le 12 then a.cc else 0 end) as Low_cc_l12

from
(
  select 
  customer_id, 
  calendar_month, 
  date,
  max(date) as max_date format date9., 
  intck('month', date, CALCULATED max_date) as date_diff, 
  cc
  from data_3 
  group by customer_id 
) as a


group by customer_id 
order by 
customer_id, 
calendar_month
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 05:41:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-of-variable-based-on-condition/m-p/649457#M194721</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-05-21T05:41:48Z</dc:date>
    </item>
    <item>
      <title>Re: Max of variable based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-of-variable-based-on-condition/m-p/649460#M194723</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt;What are the * doing?&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;* is a multiplication. The second expression is a test and returns a Boolean value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&amp;gt;Also to get last 3 months I had to change le 3 to le 2 and le 6 to le 5 and so on. Why is that?&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I don't know what you want to test. Look at the values of DATE_DIFF.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;FONT face="courier new,courier"&gt;le 2&amp;nbsp;&lt;/FONT&gt; means 0,1,2 , which means 3 months including the max month.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 06:17:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-of-variable-based-on-condition/m-p/649460#M194723</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-05-21T06:17:04Z</dc:date>
    </item>
    <item>
      <title>Re: Max of variable based on condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-of-variable-based-on-condition/m-p/649698#M194820</link>
      <description>Hi Bart, Yip the sub-query works best but not sure why I needed it over my code. Chris code actually takes the max over all 12 months even for the le 3 and le 6.&lt;BR /&gt;&lt;BR /&gt;Thanks Bart</description>
      <pubDate>Thu, 21 May 2020 20:22:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-of-variable-based-on-condition/m-p/649698#M194820</guid>
      <dc:creator>Scott86</dc:creator>
      <dc:date>2020-05-21T20:22:49Z</dc:date>
    </item>
  </channel>
</rss>

