<?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: Variation in the number of dates in historical records in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Variation-in-the-number-of-dates-in-historical-records/m-p/913790#M360135</link>
    <description>&lt;P&gt;Thanks Patrick,&lt;/P&gt;&lt;P&gt;but the input dataset can have multiple records with the same date.&lt;/P&gt;&lt;P&gt;For example&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;dt_rif&lt;/TD&gt;&lt;TD&gt;Value_1&lt;/TD&gt;&lt;TD&gt;Value_2&lt;/TD&gt;&lt;TD&gt;Desc_1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;31mar2023&lt;/TD&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;TD&gt;4343&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;31mar2023&lt;/TD&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;TD&gt;34324&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;31mar2023&lt;/TD&gt;&lt;TD&gt;233&lt;/TD&gt;&lt;TD&gt;34324&lt;/TD&gt;&lt;TD&gt;c&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;31mar2023&lt;/TD&gt;&lt;TD&gt;99&lt;/TD&gt;&lt;TD&gt;546&lt;/TD&gt;&lt;TD&gt;Desc_1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;30apr2023&lt;/TD&gt;&lt;TD&gt;54&lt;/TD&gt;&lt;TD&gt;565645&lt;/TD&gt;&lt;TD&gt;e&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;30apr2023&lt;/TD&gt;&lt;TD&gt;323&lt;/TD&gt;&lt;TD&gt;4354&lt;/TD&gt;&lt;TD&gt;f&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;31may2023&lt;/TD&gt;&lt;TD&gt;3434&lt;/TD&gt;&lt;TD&gt;45&lt;/TD&gt;&lt;TD&gt;g&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;31may2023&lt;/TD&gt;&lt;TD&gt;234&lt;/TD&gt;&lt;TD&gt;3454&lt;/TD&gt;&lt;TD&gt;h&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;31may2023&lt;/TD&gt;&lt;TD&gt;43&lt;/TD&gt;&lt;TD&gt;3455&lt;/TD&gt;&lt;TD&gt;y&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;..&lt;/TD&gt;&lt;TD&gt;..&lt;/TD&gt;&lt;TD&gt;..&lt;/TD&gt;&lt;TD&gt;..&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;The current value of the different dt_rif is 9. However, they now want to modify this value.&lt;BR /&gt;If they change from 9 to 10, the job must remove the oldest dates so that the output dataset contains the last ten distinct dates.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 31 Jan 2024 14:12:35 GMT</pubDate>
    <dc:creator>rakeon3</dc:creator>
    <dc:date>2024-01-31T14:12:35Z</dc:date>
    <item>
      <title>Variation in the number of dates in historical records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variation-in-the-number-of-dates-in-historical-records/m-p/913730#M360114</link>
      <description>&lt;P&gt;I have a dataset with&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;nine different dates&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;Every time the job is launched, if the dataset already contains&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;nine different dates&lt;/STRONG&gt;, it deletes the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;least recent date&lt;/STRONG&gt;, ensuring that there are always&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;nine dates&lt;/STRONG&gt;:&lt;/P&gt;&lt;PRE&gt;%macro versioning(FLOW=,dt_rif=);
    proc sql noprint;
        select count(distinct data_rif) into : num_ver
            from &amp;amp;FLOW
        ;
    quit;&lt;BR /&gt;    
    proc sql noprint;
        select count(*) into : num_obs
            from &amp;amp;FLOW
            where data_rif=&amp;amp;dt_rif;
    quit;&lt;BR /&gt;    
    %if &amp;amp;num_ver = 9 &amp;amp;&amp;amp; &amp;amp;num_obs=0 %then %do;&lt;BR /&gt;
        proc sql noprint;
                select min(data_rif) into : data_rif_to_del 
                    from &amp;amp;FLOW;
        quit;

        data &amp;amp;FLOW;
            set &amp;amp;FLOW;
            where data_rif ne &amp;amp;data_rif_to_del;
        run;
    %end;

%mend;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;The issue is that the number of historical dates is no longer fixed at 9; it can vary. For example, it could be 5 or 12. How can I modify the code to determine which dates (&amp;amp;data_rif_to_del) need to be deleted?&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 08:43:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variation-in-the-number-of-dates-in-historical-records/m-p/913730#M360114</guid>
      <dc:creator>rakeon3</dc:creator>
      <dc:date>2024-01-31T08:43:36Z</dc:date>
    </item>
    <item>
      <title>Re: Variation in the number of dates in historical records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variation-in-the-number-of-dates-in-historical-records/m-p/913752#M360123</link>
      <description>&lt;P&gt;Something along the line of below should work for you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  format dt_var date9.;
  do dt_var=today()-11 to today();
    output;
    output;
  end;
run;

%macro versioning(inds=,outds=, dt_rif=,keep_n_dist_dates=999999);

    %if %nrbquote(&amp;amp;outds) = %nrbquote() %then %let outds=&amp;amp;inds;

    data _null_;
      if 0 then set &amp;amp;inds(keep=&amp;amp;dt_rif);
      dcl hash h1(dataset:"&amp;amp;inds", ordered:'d');
      h1.defineKey("&amp;amp;dt_rif");
      h1.defineData("&amp;amp;dt_rif");
      h1.defineDone();
      call symputx('n_dist_dates',h1.num_items,'l');
      h1.output(dataset:'work.__dist_dates');
    run;
  
    %if &amp;amp;n_dist_dates&amp;gt;&amp;amp;keep_n_dist_dates %then
      %do;
        data _null_;
          set work.__dist_dates(firstobs=&amp;amp;keep_n_dist_dates obs=&amp;amp;keep_n_dist_dates);
          call symputx("min_dt_keep",&amp;amp;dt_rif,'l');
        run;

        data &amp;amp;outds;
          set &amp;amp;inds;
          where &amp;amp;dt_rif &amp;gt;= &amp;amp;min_dt_keep;
        run;
      %end;
    %else %if %nrbquote(&amp;amp;inds) ne %nrbquote(&amp;amp;outds) %then
      %do;
        data &amp;amp;outds;
          set &amp;amp;inds;
        run;
      %end;

    proc datasets lib=work nolist nowarn;
      delete __dist_dates;
    quit;

%mend;

%versioning(inds=have, dt_rif=dt_var, keep_n_dist_dates=5);

proc print data=have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 11:08:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variation-in-the-number-of-dates-in-historical-records/m-p/913752#M360123</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-01-31T11:08:38Z</dc:date>
    </item>
    <item>
      <title>Re: Variation in the number of dates in historical records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variation-in-the-number-of-dates-in-historical-records/m-p/913790#M360135</link>
      <description>&lt;P&gt;Thanks Patrick,&lt;/P&gt;&lt;P&gt;but the input dataset can have multiple records with the same date.&lt;/P&gt;&lt;P&gt;For example&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;dt_rif&lt;/TD&gt;&lt;TD&gt;Value_1&lt;/TD&gt;&lt;TD&gt;Value_2&lt;/TD&gt;&lt;TD&gt;Desc_1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;31mar2023&lt;/TD&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;TD&gt;4343&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;31mar2023&lt;/TD&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;TD&gt;34324&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;31mar2023&lt;/TD&gt;&lt;TD&gt;233&lt;/TD&gt;&lt;TD&gt;34324&lt;/TD&gt;&lt;TD&gt;c&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;31mar2023&lt;/TD&gt;&lt;TD&gt;99&lt;/TD&gt;&lt;TD&gt;546&lt;/TD&gt;&lt;TD&gt;Desc_1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;30apr2023&lt;/TD&gt;&lt;TD&gt;54&lt;/TD&gt;&lt;TD&gt;565645&lt;/TD&gt;&lt;TD&gt;e&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;30apr2023&lt;/TD&gt;&lt;TD&gt;323&lt;/TD&gt;&lt;TD&gt;4354&lt;/TD&gt;&lt;TD&gt;f&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;31may2023&lt;/TD&gt;&lt;TD&gt;3434&lt;/TD&gt;&lt;TD&gt;45&lt;/TD&gt;&lt;TD&gt;g&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;31may2023&lt;/TD&gt;&lt;TD&gt;234&lt;/TD&gt;&lt;TD&gt;3454&lt;/TD&gt;&lt;TD&gt;h&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;31may2023&lt;/TD&gt;&lt;TD&gt;43&lt;/TD&gt;&lt;TD&gt;3455&lt;/TD&gt;&lt;TD&gt;y&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;..&lt;/TD&gt;&lt;TD&gt;..&lt;/TD&gt;&lt;TD&gt;..&lt;/TD&gt;&lt;TD&gt;..&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;The current value of the different dt_rif is 9. However, they now want to modify this value.&lt;BR /&gt;If they change from 9 to 10, the job must remove the oldest dates so that the output dataset contains the last ten distinct dates.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 14:12:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variation-in-the-number-of-dates-in-historical-records/m-p/913790#M360135</guid>
      <dc:creator>rakeon3</dc:creator>
      <dc:date>2024-01-31T14:12:35Z</dc:date>
    </item>
    <item>
      <title>Re: Variation in the number of dates in historical records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variation-in-the-number-of-dates-in-historical-records/m-p/913844#M360137</link>
      <description>&lt;P&gt;Because your requirements are only partially clear to me, let me propose a much simpler way to go about this.&amp;nbsp; Start with sorting your incoming data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=&amp;amp;FLOW;
   by descending dt_rif;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then select how many distinct dates you would like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let n_select = 10;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then because the data is sorted from highest to lowest, select the first 10 dates you encounter:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set &amp;amp;FLOW;
   by descending dt_rif;
   if first.dt_rif then date_count + 1;
   if date_count &amp;gt; &amp;amp;n_select then stop;&lt;BR /&gt;   drop date_count;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This may do exactly what you want.&amp;nbsp; But if not, it should give us a way to discuss what you would like to achieve here.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 16:49:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variation-in-the-number-of-dates-in-historical-records/m-p/913844#M360137</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-01-31T16:49:52Z</dc:date>
    </item>
    <item>
      <title>Re: Variation in the number of dates in historical records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Variation-in-the-number-of-dates-in-historical-records/m-p/913890#M360157</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/461193"&gt;@rakeon3&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks Patrick,&lt;/P&gt;
&lt;P&gt;but the input dataset can have multiple records with the same date.&lt;/P&gt;
&lt;P&gt;....&lt;BR /&gt;The current value of the different dt_rif is 9. However, they now want to modify this value.&lt;BR /&gt;If they change from 9 to 10, the job must remove the oldest dates so that the output dataset contains the last ten distinct dates.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/461193"&gt;@rakeon3&lt;/a&gt;&amp;nbsp;That's what the macro I've shared is doing. The HAVE dataset created for testing contains already two rows per date.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 22:20:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Variation-in-the-number-of-dates-in-historical-records/m-p/913890#M360157</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-01-31T22:20:38Z</dc:date>
    </item>
  </channel>
</rss>

