<?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 Select max date as of date from another table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-max-date-as-of-date-from-another-table/m-p/582659#M165773</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Suppose I have two tables:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Transactions:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;PRODUCT TRAN_DATE&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 05JUL19&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 07JUL19&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 01AUG19&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 04APR19&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 21MAY19&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 16JUN19&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Prices:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;PRODUCT PRICE_DATE PRICE&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01JAN19&amp;nbsp; &amp;nbsp; 10&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;06JUL19&amp;nbsp; &amp;nbsp; 11&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01JAN19&amp;nbsp; &amp;nbsp; 1000&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01MAY19&amp;nbsp; &amp;nbsp; 1100&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01AUG19&amp;nbsp; &amp;nbsp; 1200&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want the price in effect as of the transaction date. So the price that corresponds to the maximum price_date that is less than or equal to the tran_date. What is the correct SQL to return a table like below?&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;PRODUCT TRAN_DATE PRICE_DATE PRICE&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 05JUL19&amp;nbsp; &amp;nbsp;01JAN19&amp;nbsp; &amp;nbsp; 10&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 07JUL19&amp;nbsp; &amp;nbsp;06JUL19&amp;nbsp; &amp;nbsp; 11&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 01AUG19&amp;nbsp; &amp;nbsp;06JUL19&amp;nbsp; &amp;nbsp; 11&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 04APR19&amp;nbsp; &amp;nbsp;01JAN19&amp;nbsp; &amp;nbsp; 1000&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 21MAY19&amp;nbsp; &amp;nbsp;01MAY19&amp;nbsp; &amp;nbsp; 1100&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 16JUN19&amp;nbsp; &amp;nbsp;01MAY19&amp;nbsp; &amp;nbsp; 1100&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Aaron&lt;/P&gt;</description>
    <pubDate>Wed, 21 Aug 2019 04:38:25 GMT</pubDate>
    <dc:creator>AaronEvans</dc:creator>
    <dc:date>2019-08-21T04:38:25Z</dc:date>
    <item>
      <title>Select max date as of date from another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-max-date-as-of-date-from-another-table/m-p/582659#M165773</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Suppose I have two tables:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Transactions:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;PRODUCT TRAN_DATE&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 05JUL19&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 07JUL19&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 01AUG19&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 04APR19&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 21MAY19&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 16JUN19&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Prices:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;PRODUCT PRICE_DATE PRICE&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01JAN19&amp;nbsp; &amp;nbsp; 10&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;06JUL19&amp;nbsp; &amp;nbsp; 11&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01JAN19&amp;nbsp; &amp;nbsp; 1000&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01MAY19&amp;nbsp; &amp;nbsp; 1100&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01AUG19&amp;nbsp; &amp;nbsp; 1200&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want the price in effect as of the transaction date. So the price that corresponds to the maximum price_date that is less than or equal to the tran_date. What is the correct SQL to return a table like below?&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;PRODUCT TRAN_DATE PRICE_DATE PRICE&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 05JUL19&amp;nbsp; &amp;nbsp;01JAN19&amp;nbsp; &amp;nbsp; 10&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 07JUL19&amp;nbsp; &amp;nbsp;06JUL19&amp;nbsp; &amp;nbsp; 11&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 01AUG19&amp;nbsp; &amp;nbsp;06JUL19&amp;nbsp; &amp;nbsp; 11&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 04APR19&amp;nbsp; &amp;nbsp;01JAN19&amp;nbsp; &amp;nbsp; 1000&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 21MAY19&amp;nbsp; &amp;nbsp;01MAY19&amp;nbsp; &amp;nbsp; 1100&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 16JUN19&amp;nbsp; &amp;nbsp;01MAY19&amp;nbsp; &amp;nbsp; 1100&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Aaron&lt;/P&gt;</description>
      <pubDate>Wed, 21 Aug 2019 04:38:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-max-date-as-of-date-from-another-table/m-p/582659#M165773</guid>
      <dc:creator>AaronEvans</dc:creator>
      <dc:date>2019-08-21T04:38:25Z</dc:date>
    </item>
    <item>
      <title>Re: Select max date as of date from another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-max-date-as-of-date-from-another-table/m-p/582661#M165775</link>
      <description>&lt;P&gt;Does it have to be an SQL solution?&lt;/P&gt;</description>
      <pubDate>Wed, 21 Aug 2019 04:56:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-max-date-as-of-date-from-another-table/m-p/582661#M165775</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-08-21T04:56:36Z</dc:date>
    </item>
    <item>
      <title>Re: Select max date as of date from another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-max-date-as-of-date-from-another-table/m-p/582666#M165778</link>
      <description>Hi- it does not have to be SQL.&lt;BR /&gt;&lt;BR /&gt;I have about 500k records in my transaction table and 300k in the price table. I coded a brute force solution that involves joining every available product price to every transaction by product. From there I sort by dates and choose from valid dates. My brute force approach takes a long time to run and I thought there must be a more elegant solution that runs faster.&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;Aaron&lt;BR /&gt;</description>
      <pubDate>Wed, 21 Aug 2019 05:37:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-max-date-as-of-date-from-another-table/m-p/582666#M165778</guid>
      <dc:creator>AaronEvans</dc:creator>
      <dc:date>2019-08-21T05:37:16Z</dc:date>
    </item>
    <item>
      <title>Re: Select max date as of date from another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-max-date-as-of-date-from-another-table/m-p/582670#M165779</link>
      <description>&lt;P&gt;Still brute force but probably faster&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Transactions;
input PRODUCT TRAN_DATE :date7.;
format TRAN_DATE date7.;
datalines;
1 05JUL19
1 07JUL19
1 01AUG19
2 04APR19
2 21MAY19
2 16JUN19
;

data Prices;
input PRODUCT PRICE_DATE :date7. PRICE;
format PRICE_DATE date7.;
datalines;
1 01JAN19 10
1 06JUL19 11
2 01JAN19 1000
2 01MAY19 1100
2 01AUG19 1200
;

data temp;
    set Prices;
    by PRODUCT;
    if not (last.PRODUCT) then do;
        p=_N_+1;
        set Prices(keep=PRICE_DATE rename=(PRICE_DATE=_PRICE_DATE)) point=p;
    end;
    else _PRICE_DATE=today();
run;

proc sql;
    create table want as
    select t.*, PRICE_DATE, PRICE
    from transactions t, temp p
    where t.PRODUCT=p.PRODUCT &amp;amp; PRICE_DATE le TRAN_DATE le _PRICE_DATE;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Aug 2019 06:05:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-max-date-as-of-date-from-another-table/m-p/582670#M165779</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-08-21T06:05:35Z</dc:date>
    </item>
    <item>
      <title>Re: Select max date as of date from another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-max-date-as-of-date-from-another-table/m-p/582676#M165782</link>
      <description>&lt;P&gt;Create a new price table from its origin having: product ID, price_start_date, price_end_date, price.&lt;/P&gt;
&lt;P&gt;Use next code to create it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=prices out=temp_pr;
        by product descending price_date;
run;

data new_prices;
  set temp_pr;
       by product;
           retain price_start_date price_end_date;
           drop price_date;
           if first.product then do;    /* current price */
              price_start_date = price_date;
              price_end_date = '31dec2999'd;  /* any high date */
              output;
         end; else do;
              if last.product then 
                 price_start_date = .;
			  else 
			     price_start_date = price_end_date +1;
              price_end_date = price_date;
			  output;
		 end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now that you have both dates, starting and ending, its easier to be used by sql:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
     create table wand as
     select a.*, p.price
    from transactions a
    left join new_prices
    on a.product = b.product and
    tran_date between price_start_date and price_end_date.
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Aug 2019 06:33:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-max-date-as-of-date-from-another-table/m-p/582676#M165782</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-08-21T06:33:14Z</dc:date>
    </item>
    <item>
      <title>Re: Select max date as of date from another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-max-date-as-of-date-from-another-table/m-p/582685#M165788</link>
      <description>&lt;P&gt;Just an idea:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;sort price by product and descending price_date&lt;/LI&gt;
&lt;LI&gt;use two temporary arrays, load price and price_date into those arrays per product&lt;/LI&gt;
&lt;LI&gt;use a loop to find the first price_date &amp;lt;= tran_date&lt;/LI&gt;
&lt;LI&gt;retain the index and use that for further processing&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Wed, 21 Aug 2019 06:50:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-max-date-as-of-date-from-another-table/m-p/582685#M165788</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-08-21T06:50:28Z</dc:date>
    </item>
    <item>
      <title>Re: Select max date as of date from another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-max-date-as-of-date-from-another-table/m-p/582735#M165808</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Transactions;
input PRODUCT TRAN_DATE :date7.;
format TRAN_DATE date7.;
datalines;
1 05JUL19
1 07JUL19
1 01AUG19
2 04APR19
2 21MAY19
2 16JUN19
;

data Prices;
input PRODUCT PRICE_DATE :date7. PRICE;
format PRICE_DATE date7.;
datalines;
1 01JAN19 10
1 06JUL19 11
2 01JAN19 1000
2 01MAY19 1100
2 01AUG19 1200
;


proc sql;
     create table want as
   select a.*,b.price_date, b.price
    from transactions a
    left join prices b
    on a.product = b.product and tran_date &amp;gt;= price_date
	group by a.product, a.tran_date
	having price_date=max(price_date);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Aug 2019 11:57:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-max-date-as-of-date-from-another-table/m-p/582735#M165808</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-08-21T11:57:59Z</dc:date>
    </item>
    <item>
      <title>Re: Select max date as of date from another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-max-date-as-of-date-from-another-table/m-p/582808#M165841</link>
      <description>&lt;P&gt;I discarded some of the ideas i had earlier. First two steps are for setting the size of the temp arrays. Performance has to be tested.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=work.prices noprint order=freq;
   table product / nopercent out=work.ProductCount(keep=product count) ;
run;

data _null_;
   set work.ProductCount(obs=1);
   call symputx('maxElements', count);
run;


data want(keep=product tran_date price_date price);
   if 0 then set work.Transactions;
   array d[&amp;amp;maxElements.] _temporary_;
   array p[&amp;amp;maxElements.] _temporary_;

   call missing(of d[*]);
   call missing(of p[*]);

   do i = 1 to dim(d) until (last.product);
      set work.Prices;
      by product;

      d[i] = price_date;
      p[i] = price;
   end;

   c = i;
   s = 1;

   do until(last.product);
      set work.Transactions;
      by product;

      do i = s to c while(d[i] &amp;lt;= tran_date);
      end;

      i = i-1;
      s = i;
      price_date = d[i];
      price = p[i];

      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Aug 2019 14:15:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-max-date-as-of-date-from-another-table/m-p/582808#M165841</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-08-21T14:15:41Z</dc:date>
    </item>
  </channel>
</rss>

