<?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 per loan in last 7 days in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974381#M377900</link>
    <description>&lt;P&gt;As I said, the array is&amp;nbsp;&lt;EM&gt;date-indexed&lt;/EM&gt;. Now, with your knowledge about SAS dates, think which dates a range of 1 to 30000 covers.&lt;/P&gt;
&lt;P&gt;The array needs to span the range of dates present in your data, or more.&lt;/P&gt;</description>
    <pubDate>Mon, 08 Sep 2025 07:30:10 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2025-09-08T07:30:10Z</dc:date>
    <item>
      <title>Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974333#M377884</link>
      <description>&lt;P&gt;Hello&lt;BR /&gt;Let's say I have a data set in long structure. for each customer ID +loan need to calculate max offer in last 7 days prior to loan date&lt;BR /&gt;What is the way to do it?&lt;BR /&gt;For example for customer 111 there us one loan and max 7 days before is 22000( offer in 01jul is not relevant because more than 7 days).&lt;BR /&gt;For customer 222 loan 13000 max offer is 40000 and for loan 35000 max is 28000 ( Offer 40000 is not taking into account because more than 7 days prior loan)&lt;BR /&gt;&lt;BR /&gt;In desire table will have 3 rows because have 3 loans.&lt;BR /&gt;&lt;BR /&gt;What is way yo do it??&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Format date ddmmyy10.;
Input custid date :date9. loansAmnt offerAmnt;
Cards;
111 01JUL2025 . 50000
111 03jul2025 . 22000
111 08jul2025 . 19000
111 09jul2025 5000 .
222 01jul2025 . 40000
222 03jul2025 . 28000
222 04jul2025 13000 .
222 08jul2025 . 27000
223 09jul2025 35000 .
;
Run:&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 07 Sep 2025 07:05:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974333#M377884</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2025-09-07T07:05:38Z</dc:date>
    </item>
    <item>
      <title>Re: Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974335#M377885</link>
      <description>&lt;P&gt;Use a date-indexed array to keep the offer amounts:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by custid;
array ams [1:30000] _temporary_;
if first.custid
then do _date = 1 to 30000;
  ams[_date] = 0;
end;
if offeramnt ne . then ams[date] = offeramnt;
if loansamnt ne .
then do _date = date - 7 to date;
  put _date ams[_date];
  maxamnt = max(maxamnt,ams[_date]);
end;
if loansamnt;
drop _date offeramnt;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 07 Sep 2025 10:00:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974335#M377885</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-09-07T10:00:15Z</dc:date>
    </item>
    <item>
      <title>Re: Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974336#M377886</link>
      <description>&lt;P&gt;PS if you want a non-zero value for the last observation, custid should be 222 there, not 223.&lt;/P&gt;</description>
      <pubDate>Sun, 07 Sep 2025 10:30:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974336#M377886</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-09-07T10:30:25Z</dc:date>
    </item>
    <item>
      <title>Re: Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974348#M377891</link>
      <description>&lt;P&gt;Here's a data step that effectively maintains a moving window of offer amounts for the prior 7 days.&lt;BR /&gt;&amp;nbsp; With each obs, first weed out stale (over 7 days prior) data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Then just take the maximum of that small array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Then update the arrays with the new obs&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  format date ddmmyy10.;
  input custid date :date9. loansAmnt offerAmnt;
cards;
111 01JUL2025 .     50000
111 03jul2025 .     22000
111 08jul2025 .     19000
111 09jul2025 5000  .
222 01jul2025 .     40000
222 03jul2025 .     28000
222 04jul2025 13000 .
222 08jul2025 .     27000
223 09jul2025 35000 .
run;
data want (drop=_:);
  set have;
  by custid;

  array tempdat{0:6} _temporary_; /*Observed dates within prior 7 days*/
  array tempval{0:6} _temporary_; /*Observed offers within prior 7 days*/  

  /* Weed stale data first */
  if first.custid=1 then call missing(of tempval{*},of tempdat{*});
  else do while (.&amp;lt; min(of tempdat{*}) &amp;lt; date-7);    
    _d=mod(min(of tempdat{*}),7);
    call missing(tempval{_d},tempdat{_d});
  end;

  /* Get the maximum */
  if n(of tempdat{*})&amp;gt;0 then max_prior_7days=max(of tempval{*});

  /*Update the arrays with current obs */
  _d=mod(date,7);   
  tempdat{_d}=date;
  tempval{_d}=offeramnt;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ignore the previously sumitted code below, which didn't weed before taking the maximum:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRIKE&gt;data want (drop=_:);&lt;/STRIKE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRIKE&gt;&amp;nbsp; set have; by custid; &lt;/STRIKE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRIKE&gt;&amp;nbsp; array tempdat{0:6} _temporary_; /*Observed dates within prior 7 days*/ &lt;/STRIKE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRIKE&gt;&amp;nbsp; array tempval{0:6} _temporary_; /*Observed offers within prior 7 days*/ &lt;/STRIKE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRIKE&gt;&amp;nbsp; /* Get the maximum, when there is a 7-day window available */ &lt;/STRIKE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRIKE&gt;&amp;nbsp; if first.custid=0 and dif(date)&amp;lt;=7 then max_prior_7days=max(of tempval{*}); &lt;/STRIKE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRIKE&gt;&amp;nbsp; else call missing(of tempval{*},of tempdat{*}); &lt;/STRIKE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRIKE&gt;&amp;nbsp; /*Update the arrays with current obs */ &lt;/STRIKE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRIKE&gt;&amp;nbsp; _d=mod(date,7); tempdat{_d}=date; &lt;/STRIKE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRIKE&gt;&amp;nbsp; tempval{_d}=offeramnt; &lt;/STRIKE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRIKE&gt;&amp;nbsp; /*Weed out stale data */ &lt;/STRIKE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRIKE&gt;&amp;nbsp; do while (min(of tempdat{*})&amp;lt;date-7); &lt;/STRIKE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRIKE&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; _d=mod(min(of tempdat{*}),7); &lt;/STRIKE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRIKE&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; call missing(tempval{_d},tempdat{_d}); &lt;/STRIKE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRIKE&gt;&amp;nbsp; end; &lt;/STRIKE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRIKE&gt;run;&lt;/STRIKE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Two points:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The temporary arrays are indexed from 0 to 6, effectively representing a day-of-week index.&amp;nbsp; So the data in the arrays are not ordered strictly chronologically, but order doesn't matter for getting the maximum.&amp;nbsp; The task is to be sure to eliminate stale values.&lt;/LI&gt;
&lt;LI&gt;The DIF(x) function is&amp;nbsp;&amp;nbsp; x-lag(x).&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Sun, 07 Sep 2025 20:49:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974348#M377891</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2025-09-07T20:49:58Z</dc:date>
    </item>
    <item>
      <title>Re: Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974376#M377895</link>
      <description>Why array have 6 arguments?</description>
      <pubDate>Mon, 08 Sep 2025 06:24:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974376#M377895</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2025-09-08T06:24:54Z</dc:date>
    </item>
    <item>
      <title>Re: Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974377#M377896</link>
      <description>Why array have 30000 arguments? How dud you know to choose this number ?</description>
      <pubDate>Mon, 08 Sep 2025 06:25:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974377#M377896</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2025-09-08T06:25:43Z</dc:date>
    </item>
    <item>
      <title>Re: Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974381#M377900</link>
      <description>&lt;P&gt;As I said, the array is&amp;nbsp;&lt;EM&gt;date-indexed&lt;/EM&gt;. Now, with your knowledge about SAS dates, think which dates a range of 1 to 30000 covers.&lt;/P&gt;
&lt;P&gt;The array needs to span the range of dates present in your data, or more.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Sep 2025 07:30:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974381#M377900</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-09-08T07:30:10Z</dc:date>
    </item>
    <item>
      <title>Re: Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974398#M377902</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Why array have 30000 arguments? How dud you know to choose this number ?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you are using an array from 0 to 30000, it covers Jan 1, 1960 through 19feb2024.&amp;nbsp; You can customize the date range per below (say for 01jan2009 through 31dec2020):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let begdate=01jan2009 ;
%let enddate=31dec2020 ;

data;
  * other sas code ;
  array history {%sysevalf("&amp;amp;begdate"d):%sysevalf("&amp;amp;enddate"d)} _temporary_ ;
  * other sas code ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Sep 2025 11:08:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974398#M377902</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2025-09-08T11:08:19Z</dc:date>
    </item>
    <item>
      <title>Re: Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974400#M377903</link>
      <description>&lt;P&gt;Two simple steps&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Find date of loan and find those offers within 7 days of loan (much simpler data step than those shown by others)&lt;/LI&gt;
&lt;LI&gt;Find max of offers within last 7 days (PROC SUMMARY);&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* step 1 */
data intermediate;
    merge have(where=(loansamnt=.))  have(where=(loansamnt&amp;gt;.) drop=offeramnt rename=(date=loandate));
    by custid;
    delta_date=loandate-date;
run;

/* step 2 */
proc summary data=intermediate nway;
    where delta_date between 0 and 7 or missing(delta_date);
    class custid;
    var offeramnt;
    output out=max_offer max=max_offer;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Sep 2025 11:17:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974400#M377903</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-09-08T11:17:16Z</dc:date>
    </item>
    <item>
      <title>Re: Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974404#M377904</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;, I think the major or first problem for solve this question is, &lt;STRONG&gt;create one more &lt;EM&gt;date column&lt;/EM&gt; for your &lt;EM&gt;loan date&lt;/EM&gt;&lt;/STRONG&gt;. You cannot put the date for loans and date for offer in only one date column, this is not good, because both dates will be needed for calculating and further analysis. Put two different kind of date in one column will make date calculation difficult and cause trouble in future data analysis, regardless it is business or health data. Therefore, I suggest that create a &lt;EM&gt;loandate&lt;/EM&gt; and an &lt;EM&gt;offerdate&lt;/EM&gt;&amp;nbsp; column for your dataset first, and the problem will become much easier by using whatever &lt;EM&gt;procs&lt;/EM&gt; with an calcualtion such as&lt;EM&gt; offerdate-loandate&amp;lt;=7 days;&lt;/EM&gt; statement.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Sep 2025 11:51:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974404#M377904</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-09-08T11:51:34Z</dc:date>
    </item>
    <item>
      <title>Re: Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974406#M377905</link>
      <description>&lt;P&gt;Your calculation is off by quite some years. 30000 days is equivalent to ~ 82 years (30000 / 365.25 = 82.1355), so the end date must be in the 2040's, not in 2024.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Sep 2025 12:14:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974406#M377905</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-09-08T12:14:15Z</dc:date>
    </item>
    <item>
      <title>Re: Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974443#M377909</link>
      <description>Can you show full code  how to create desired output using 2 dare columns?</description>
      <pubDate>Mon, 08 Sep 2025 13:55:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974443#M377909</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2025-09-08T13:55:46Z</dc:date>
    </item>
    <item>
      <title>Re: Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974633#M377960</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;, this is what I come up with today, I have not figure out the final solution (the three row table with max offer amount within 7 days). A question is what is the &lt;EM&gt;custid&lt;/EM&gt; for the last row, is it &lt;EM&gt;222&lt;/EM&gt; or &lt;EM&gt;223&lt;/EM&gt;? I assume it is &lt;EM&gt;222&lt;/EM&gt;, i.e., &lt;EM&gt;custid 222&lt;/EM&gt; has two loans. The modified dataset and code is as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Format date ddmmyy10.;
Input custid date :date9. loansAmnt offerAmnt;
Cards;
111 01JUL2025 . 50000
111 03jul2025 . 22000
111 08jul2025 . 19000
111 09jul2025 5000 .
222 01jul2025 . 40000
222 03jul2025 . 28000
222 04jul2025 13000 .
222 08jul2025 . 27000
222 09jul2025 35000 .
;
Run;
proc print data=have;run;
data have1;
   set have;
   if loansamnt=. then offerdate=date;
      else if loansamnt^=. then loandate=date;
   format offerdate loandate date9.;
run;
proc print data=have1;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_0-1757429315804.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/109800iB60B9823C57A7FE9/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_0-1757429315804.png" alt="dxiao2017_0-1757429315804.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Sep 2025 15:36:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974633#M377960</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-09-09T15:36:47Z</dc:date>
    </item>
    <item>
      <title>Re: Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974643#M377962</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;, (continued from my last thread) if &lt;EM&gt;custid 222&lt;/EM&gt; has two loans (i.e., that &lt;EM&gt;custid&lt;/EM&gt; has two load dates), then two loan date columns (instead of one loan date column) need to be created, that consumes more steps, which means your original dataset although looks good, but in fact it is far, far away from ready for analysis, because you need to create three date/time columns from one date column and match them back into the original dataset you have. Here is the next two steps which create two loan date columns (and then, next, one may use &lt;EM&gt;proc sql&lt;/EM&gt; to join the tables to produce a final analysis dataset that is really ready for further data analysis):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table have2 as
select custid,
       loandate
   from have1
   where loandate^=.;
select * from have2;
quit;
proc transpose data=have2 out=have3;
   by custid;
   var loandate;
run;
proc print data=have3;run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_0-1757432906071.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/109805i1526548FCAFEE9B4/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_0-1757432906071.png" alt="dxiao2017_0-1757432906071.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Sep 2025 15:50:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974643#M377962</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-09-09T15:50:24Z</dc:date>
    </item>
    <item>
      <title>Re: Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974704#M377965</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Why array have 6 arguments?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The array does NOT have six elements, it has seven elements, where the lower bound is zero (not one) and the upper bound is six.&amp;nbsp; When you divide the date value by 7 and keep the remainder, you will end up with 0, 1, ..., 5, or 6.&amp;nbsp; So each day of the week can trivially be placed in exactly one element of the array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Sep 2025 03:19:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974704#M377965</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2025-09-10T03:19:02Z</dc:date>
    </item>
    <item>
      <title>Re: Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974836#M377989</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;, in fact, two loans amount columns also need to created to produce a final analysis dataset that is ready for further analysis. I have created this final analysis dataset today, the code and output tables are as follows (and I will do the date calculation and produce the final report table later on):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Format date ddmmyy10.;
Input custid date :date9. loansAmnt offerAmnt;
Cards;
111 01JUL2025 . 50000
111 03jul2025 . 22000
111 08jul2025 . 19000
111 09jul2025 5000 .
222 01jul2025 . 40000
222 03jul2025 . 28000
222 04jul2025 13000 .
222 08jul2025 . 27000
222 09jul2025 35000 .
;
Run;
proc print data=have;title "have";run;
/*create two loan date columns*/
data have1;
   set have;
   if loansamnt=. then offerdate=date;
      else if loansamnt^=. then loandate=date;
   format offerdate loandate date9.;
run;
proc print data=have1;title "have1";run;
proc sql;
create table have2 as
select custid,
       loandate
   from have1
   where loandate^=.;
title "have2";
select * from have2;
quit;
proc transpose data=have2 
               out=have3(drop=_name_
                         rename=(col1=loandate1
                                 col2=loandate2)
                         );
   by custid;
   var loandate;
run;
proc print data=have3;title "have3";run;
/*create two loans amount columns*/
proc sql;
create table have4 as
select custid,
       loansamnt
   from have1
   where loansamnt^=.;
title "have4";
select * from have4;
quit;
proc transpose data=have4
               out=have5(drop=_name_
                          rename=(col1=loansamnt1
                                  col2=loansamnt2)
                         );
   by custid;
   var loansamnt;
run;
proc print data=have5;title "have5";run;
/*produce final analysis dataset that
is ready for data analysis*/
proc sql;
create table want1 as
select h1.custid,
       h1.offerdate,
       h1.offeramnt,
       h3.loandate1,
       h5.loansamnt1,
       h3.loandate2,
       h5.loansamnt2
   from have1 as h1 left join
        have3 as h3
   on h1.custid=h3.custid left join
      have5 as h5
   on h1.custid=h5.custid
   where h1.offeramnt^=. and
         h1.offerdate^=.
   order by 1,2;
select * from want1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_0-1757605082728.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/109892i4CCE45058213E96A/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_0-1757605082728.png" alt="dxiao2017_0-1757605082728.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_1-1757605167897.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/109893iC6FF85A5EFFF18DF/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_1-1757605167897.png" alt="dxiao2017_1-1757605167897.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The final analysis dataset (that is ready for data analysis per your request) look like this:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_2-1757605276404.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/109894i8CC7EF41D1957C45/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_2-1757605276404.png" alt="dxiao2017_2-1757605276404.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Sep 2025 15:42:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/974836#M377989</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-09-11T15:42:29Z</dc:date>
    </item>
    <item>
      <title>Re: Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/975067#M378036</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp; ,have you come up with your final answer? I have my solution here (and this is continued from my last thread and step, which was create analysis dataset), today the final step is using &lt;EM&gt;proc sql union&lt;/EM&gt; to produce the final report. I think the code in my last thread(analysis dateset) and this thread(final report) could be the answer to your question. &lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;Please kindly let me know if I answer your question, thanks! The code and output are as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*create separate dataset for
loan1 and loan2, and calculate date
and select rows according to requirement*/
proc sql;
create table want2 as
select custid,
       offerdate,
       offeramnt,
       loandate1,
       loansamnt1,
       intnx('day',loandate1,-7)
          as loand1calc format=date9.
   from want1;
select * from want2;
quit;
proc sql;
create table want3 as
select custid,
       offerdate,
       offeramnt,
       loandate2,
       loansamnt2,
       intnx('day',loandate2,-7)
          as loand2calc format=date9.
   from want1
   where loandate2 ^=. and
         loansamnt2 ^=.;
select * from want3;
quit;
data want2a;
   set want2;
   if offerdate&amp;gt;loand1calc;
run;
proc print data=want2a;run;
data want3a;
   set want3;
   if offerdate&amp;gt;loand2calc;
run;
proc print data=want3a;run;&lt;BR /&gt;
/*create final report table 
per request using sql subquery and sql union*/&lt;BR /&gt;
proc sql;
select custid,
       offerdate,
       offeramnt,
       loandate1 as loandate
                format=date9.,
       loansamnt1 as loansamnt
   from want2a
   where offeramnt in(select max(offeramnt) 
                         from want2a
                         group by custid)
union
select custid,
       offerdate,
       offeramnt,
       loandate2 as loandate
               format=date9.,
       loansamnt2 as loansamnt
   from want3a
   where offeramnt in(select max(offeramnt) 
                         from want3a
                         group by custid);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_0-1757939634832.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/109961i5DC4FCE749A641F0/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_0-1757939634832.png" alt="dxiao2017_0-1757939634832.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_1-1757939705542.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/109962iE7FBBA9FFBFEAAC4/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_1-1757939705542.png" alt="dxiao2017_1-1757939705542.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_3-1757939794742.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/109964iE2823F42C1358E46/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_3-1757939794742.png" alt="dxiao2017_3-1757939794742.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Sep 2025 12:48:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/975067#M378036</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-09-15T12:48:10Z</dc:date>
    </item>
    <item>
      <title>Re: Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/975079#M378038</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/466238"&gt;@dxiao2017&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;Please run this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Format date ddmmyy10.;
Input custid date :date9. loansAmnt offerAmnt;
Cards;
111 01JUL2025 . 50000
111 03jul2025 . 22000
111 08jul2025 . 19000
111 09jul2025 5000 .
222 01jul2025 . 40000
222 03jul2025 . 28000
222 04jul2025 13000 .
222 08jul2025 . 27000
222 09jul2025 35000 .
;

data want;
set have;
by custid;
array ams [1:30000] _temporary_;
if first.custid
then do _date = 1 to 30000;
  ams[_date] = 0;
end;
if offeramnt ne . then ams[date] = offeramnt;
if loansamnt ne .
then do _date = date - 7 to date;
  put _date ams[_date];
  if ams[_date] &amp;gt; maxamnt
  then do;
    maxamnt = ams[_date];
    offerdate = _date;
  end;
end;
if loansamnt;
drop _date offeramnt;
format offerdate ddmmyy10.;
run;

proc print data=want noobs;
var custid offerdate maxamnt date loansamnt;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then compare the results with yours, and the respective complexity of the codes.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Sep 2025 13:27:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/975079#M378038</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-09-15T13:27:29Z</dc:date>
    </item>
    <item>
      <title>Re: Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/975107#M378041</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;, thanks a lot for your suggestion, I run your code and it works perfect &lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;, I copy and paste the code and result here:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Format date ddmmyy10.;
Input custid date :date9. loansAmnt offerAmnt;
Cards;
111 01JUL2025 . 50000
111 03jul2025 . 22000
111 08jul2025 . 19000
111 09jul2025 5000 .
222 01jul2025 . 40000
222 03jul2025 . 28000
222 04jul2025 13000 .
222 08jul2025 . 27000
222 09jul2025 35000 .
;
run;
proc print data=have;run;
data want;
   set have;
   by custid;
   array ams [1:30000] _temporary_;
   if first.custid then do _date = 1 to 30000;
      ams[_date] = 0;
   end;
   if offeramnt ne . then ams[date] = offeramnt;
   if loansamnt ne . then do _date = date - 7 to date;
      put _date ams[_date];
      if ams[_date] &amp;gt; maxamnt then do;
         maxamnt = ams[_date];
         offerdate = _date;
      end;
   end;
   if loansamnt;
   drop _date offeramnt;
   format offerdate ddmmyy10.;
run;
proc print data=want noobs;
var custid offerdate maxamnt date loansamnt;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_0-1757954512508.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/109988i946AE7B82B579821/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dxiao2017_0-1757954512508.png" alt="dxiao2017_0-1757954512508.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I wrote my code that way just in case my job decides how much to pay me according to how many rows of code I can write for a project&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;, I am kidding. I do not have a job now and want to find one. I think your code works perfect. But the logic, syntax, and techniques are difficult to understand. A lot of experience and practice is needed before one can write code like this. Could you explain the logic of your code a little bit&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;, thanks a lot!&lt;/P&gt;</description>
      <pubDate>Mon, 15 Sep 2025 16:58:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/975107#M378041</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-09-15T16:58:40Z</dc:date>
    </item>
    <item>
      <title>Re: Max per loan in last 7 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/975159#M378050</link>
      <description>&lt;P&gt;The key element is an array used to store date-related values. Keep in mind that dates are counts of days and can therefore easily be used as indexes into an array. For all practical date values, the size of the array will be rather small (my 30000 element array is just 240K).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* create want */
data want;
/* read have */
set have;
/* group by custid */
by custid;
/* define the array */
array ams [1:30000] _temporary_;
/* clear the array at the start of a new group */
if first.custid
then do _date = 1 to 30000;
  ams[_date] = 0;
end;
/* store offer amount for a given date in the array */
if offeramnt ne . then ams[date] = offeramnt;
/* if a loan is given, iterate through the array for the preceding 7 days */
if loansamnt ne .
then do _date = date - 7 to date;
  /* determine the maximum offer amount, and keep the date from it */
  /* maxamnt is always missing before the DO loop starts */
  if ams[_date] &amp;gt; maxamnt
  then do;
    maxamnt = ams[_date];
    offerdate = _date;
  end;
end;
/* only proceed further (to the implicit OUTPUT) when a loan was given */
if loansamnt;
drop _date offeramnt;
format offerdate ddmmyy10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Sep 2025 11:33:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Max-per-loan-in-last-7-days/m-p/975159#M378050</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-09-16T11:33:42Z</dc:date>
    </item>
  </channel>
</rss>

