<?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: Help to choose max date based on priority in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-to-choose-max-date-based-on-priority/m-p/399407#M278485</link>
    <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE1;
   input CASE ACTION_IN_DATE date9. ACTION_OUT_DATE : date9. PRIORITY;
   format ACTION_IN_DATE ACTION_OUT_DATE  date9.;
   cards;
100 28MAY2015 29SEP2015 02
100 04SEP2015 27NOV2015 03
100 27NOV2015 17SEP2016 04
run;

data HAVE2;
  input CASE  POST_DATE date9. AMOUNT;
  format POST_DATE   date9.;
  ID+1;
  cards;
100 18SEP2015 250
100 19SEP2015 100
100 30NOV2015 300
run;
 

proc sql;
  create table GET_CARTESIAN as
  select  have1.CASE 
        , ACTION_IN_DATE 
        , ACTION_OUT_DATE 
        , PRIORITY
        , ID
        , AMOUNT
  from HAVE1, HAVE2
  where have1.CASE=have2.CASE
    and ACTION_IN_DATE &amp;lt;= POST_DATE &amp;lt;= ACTION_OUT_DATE
  order by 1, ID, PRIORITY;
quit;

data GET_PRIORITY; 
  set GET_CARTESIAN;
  by CASE ID ;
  if last.ID;
run; 

proc sql;
  select  a.CASE 
        , ACTION_IN_DATE 
        , ACTION_OUT_DATE 
        , PRIORITY
        , sum(AMOUNT)
  from GET_PRIORITY a
  group by 1,2,3,4;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure SQL: Query Results" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;&lt;COLGROUP&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r b header" scope="col"&gt;CASE&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;ACTION_IN_DATE&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;ACTION_OUT_DATE&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;PRIORITY&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;&amp;nbsp;&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;100&lt;/TD&gt;
&lt;TD class="r data"&gt;04SEP2015&lt;/TD&gt;
&lt;TD class="r data"&gt;27NOV2015&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;350&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;100&lt;/TD&gt;
&lt;TD class="r data"&gt;27NOV2015&lt;/TD&gt;
&lt;TD class="r data"&gt;17SEP2016&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="r data"&gt;300&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 28 Sep 2017 05:45:29 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2017-09-28T05:45:29Z</dc:date>
    <item>
      <title>Help to choose max date based on priority</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-to-choose-max-date-based-on-priority/m-p/399248#M278483</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I am facing a bit of dead end with the logic. Thought to post in on the forum to seek help.&lt;/P&gt;&lt;P&gt;I have the following datasets.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; input case action_in_date action_out_date priority;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; cards;&lt;BR /&gt;100 28-MAY-2015 29SEP2015 02&lt;BR /&gt;100 04-SEP-2015 27NOV2015 03&lt;BR /&gt;100 27-NOV-2015 17SEP2016 04&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data have2;&lt;BR /&gt;input case post_date amount;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; cards;&lt;BR /&gt;100 18-SEP-2015 250&lt;BR /&gt;100 19-SEP-2015 100&lt;BR /&gt;100 30-NOV-2015 300&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, the post date in first 2 rows (18-SEP-2015 and 19-SEP-2015)&amp;nbsp; in have2 dataset have a post date which falls between two date ranges in have1 dataset. i e. between&amp;nbsp; 28-MAY-2015 and 29SEP2015 and 04-SEP-2015 and&amp;nbsp; 27NOV2015. But i want the money to be allocated to highest priority. ie between 04-SEP-2015 and&amp;nbsp; 27NOV2015 which has a priority 03. The output i am looking for is below&lt;BR /&gt;&lt;BR /&gt;/*output;*/&lt;BR /&gt;100 04-SEP-2015 27NOV2015 03 350&lt;BR /&gt;100 27-NOV-2015 17SEP2016 04 300&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is highly appreciated.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2017 16:22:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-to-choose-max-date-based-on-priority/m-p/399248#M278483</guid>
      <dc:creator>sree2</dc:creator>
      <dc:date>2017-09-27T16:22:52Z</dc:date>
    </item>
    <item>
      <title>Re: Help to choose max date based on priority</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-to-choose-max-date-based-on-priority/m-p/399303#M278484</link>
      <description>&lt;P&gt;I presume you have different date ranges for difference CASE values.&amp;nbsp; If so, I think there are 3 steps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Read all the HAVE1 records for a CASE and build an array from the mindate to the maxdate indicating the highest priority, and corresponding date-group (action_in_date:action_out_date).&amp;nbsp; That is the arrays are a map from each date to the desired date group.&lt;/LI&gt;
&lt;LI&gt;Read all the HAVE2 records for the same case.&amp;nbsp; Use the map above to accumulate identify which date groups do with each post_date.&lt;/LI&gt;
&lt;LI&gt;The aggregate the amount's for each date group:&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%let lobound=%sysfunc(inputn(01may2015,date9.));
%let upbound=%sysfunc(inputn(30sep2016,date9.));

/** step 1 **/
data date_groups (keep=case action_in_date action_out_date post_date) / veiw=date_groups;
  set have1;
  by case;

  array _aidate{&amp;amp;lobound:&amp;amp;upbound} _temporary_;
  array _aodate{&amp;amp;lobound:&amp;amp;upbound} _temporary_;
  array _priorities{&amp;amp;lobound:&amp;amp;upbound} _temporary_;

  if first.case then call missing(of _aidate{*},of _aodate{*}, of _priorities{*}, min_date, max_date);

  retain min_date max_date;
  min_date=min(min_date,action_in_date);
  max_date=max(max_date,action_out_date);

  do post_date=action_in_date to action_out_date;
    if priority&amp;gt;_priorities{post_date} then do;
	  _priorities{post_date}=priority;
	  _aidate{post_date}=action_in_date;
	  _aodate{post_date}=action_out_date;
	end;
  end;

  if last.case then do post_date=min_date to max_date;
    action_in_date=_aidate{post_date};
    action_out_date=_aidate{post_date};
	output;
	format post_date date9.;
  end;
run;

/** step 2 **/
data need / view=need;
  merge date_groups have2 (in=inh);
  by case post_date;
  if inh;
run;

/** step 3 **/

proc summary data=need noprint nway ;
  class case action_in_date action_out_date ;
  var amount;
  output out=want sum(amount)=total_amount;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note I've set LOBOUND and UPBOUND to the earliest and latest expected action dates over the entire set of CASEs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All this could be done in a single DATA step, but I don't see how to do so without introducing hash objects.&amp;nbsp; This is relatively simpler to understand.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2017 19:01:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-to-choose-max-date-based-on-priority/m-p/399303#M278484</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-09-27T19:01:16Z</dc:date>
    </item>
    <item>
      <title>Re: Help to choose max date based on priority</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-to-choose-max-date-based-on-priority/m-p/399407#M278485</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE1;
   input CASE ACTION_IN_DATE date9. ACTION_OUT_DATE : date9. PRIORITY;
   format ACTION_IN_DATE ACTION_OUT_DATE  date9.;
   cards;
100 28MAY2015 29SEP2015 02
100 04SEP2015 27NOV2015 03
100 27NOV2015 17SEP2016 04
run;

data HAVE2;
  input CASE  POST_DATE date9. AMOUNT;
  format POST_DATE   date9.;
  ID+1;
  cards;
100 18SEP2015 250
100 19SEP2015 100
100 30NOV2015 300
run;
 

proc sql;
  create table GET_CARTESIAN as
  select  have1.CASE 
        , ACTION_IN_DATE 
        , ACTION_OUT_DATE 
        , PRIORITY
        , ID
        , AMOUNT
  from HAVE1, HAVE2
  where have1.CASE=have2.CASE
    and ACTION_IN_DATE &amp;lt;= POST_DATE &amp;lt;= ACTION_OUT_DATE
  order by 1, ID, PRIORITY;
quit;

data GET_PRIORITY; 
  set GET_CARTESIAN;
  by CASE ID ;
  if last.ID;
run; 

proc sql;
  select  a.CASE 
        , ACTION_IN_DATE 
        , ACTION_OUT_DATE 
        , PRIORITY
        , sum(AMOUNT)
  from GET_PRIORITY a
  group by 1,2,3,4;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure SQL: Query Results" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;&lt;COLGROUP&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r b header" scope="col"&gt;CASE&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;ACTION_IN_DATE&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;ACTION_OUT_DATE&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;PRIORITY&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;&amp;nbsp;&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;100&lt;/TD&gt;
&lt;TD class="r data"&gt;04SEP2015&lt;/TD&gt;
&lt;TD class="r data"&gt;27NOV2015&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;350&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;100&lt;/TD&gt;
&lt;TD class="r data"&gt;27NOV2015&lt;/TD&gt;
&lt;TD class="r data"&gt;17SEP2016&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="r data"&gt;300&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2017 05:45:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-to-choose-max-date-based-on-priority/m-p/399407#M278485</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-09-28T05:45:29Z</dc:date>
    </item>
    <item>
      <title>Re: Help to choose max date based on priority</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-to-choose-max-date-based-on-priority/m-p/399465#M278486</link>
      <description>&lt;P&gt;Thankyou both. Much appreciated&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2017 12:05:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-to-choose-max-date-based-on-priority/m-p/399465#M278486</guid>
      <dc:creator>sree2</dc:creator>
      <dc:date>2017-09-28T12:05:08Z</dc:date>
    </item>
  </channel>
</rss>

