<?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 Do Until statement on decreasing date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-Until-statement-on-decreasing-date/m-p/387977#M93056</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the following code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options symbolgen mprint mlogic;
options mautosource
        sasautos=(	'/my/macro/location/folder'
					sasautos
				);
libname lib "/sas/dataset/folder/path/";*this is automated;
%let path_lib = %sysfunc(pathname(lib));
%put &amp;amp;path_lib.;
%let dataset = name_of_sas_dataset_yyyymm;
%let end_date=201707;


DATA _NULL_;
   DO UNTIL(fileexist(&amp;amp;path_lib.&amp;amp;dataset._&amp;amp;end_date..sas7bdat));
      %let end_date=%eval(&amp;amp;end_date-1);
   END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;so i want to have the end_date macro variable have the value of the latest dataset.&lt;/P&gt;
&lt;P&gt;How can i have this working?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please suggest.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Mon, 14 Aug 2017 21:53:44 GMT</pubDate>
    <dc:creator>ArpitSharma</dc:creator>
    <dc:date>2017-08-14T21:53:44Z</dc:date>
    <item>
      <title>Do Until statement on decreasing date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Until-statement-on-decreasing-date/m-p/387977#M93056</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the following code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options symbolgen mprint mlogic;
options mautosource
        sasautos=(	'/my/macro/location/folder'
					sasautos
				);
libname lib "/sas/dataset/folder/path/";*this is automated;
%let path_lib = %sysfunc(pathname(lib));
%put &amp;amp;path_lib.;
%let dataset = name_of_sas_dataset_yyyymm;
%let end_date=201707;


DATA _NULL_;
   DO UNTIL(fileexist(&amp;amp;path_lib.&amp;amp;dataset._&amp;amp;end_date..sas7bdat));
      %let end_date=%eval(&amp;amp;end_date-1);
   END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;so i want to have the end_date macro variable have the value of the latest dataset.&lt;/P&gt;
&lt;P&gt;How can i have this working?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please suggest.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 14 Aug 2017 21:53:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Until-statement-on-decreasing-date/m-p/387977#M93056</guid>
      <dc:creator>ArpitSharma</dc:creator>
      <dc:date>2017-08-14T21:53:44Z</dc:date>
    </item>
    <item>
      <title>Re: Do Until statement on decreasing date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Until-statement-on-decreasing-date/m-p/387991#M93061</link>
      <description>&lt;P&gt;Why not query the SASHELP.VTABLE view instead of this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Aug 2017 21:30:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Until-statement-on-decreasing-date/m-p/387991#M93061</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-08-14T21:30:49Z</dc:date>
    </item>
    <item>
      <title>Re: Do Until statement on decreasing date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Until-statement-on-decreasing-date/m-p/387998#M93064</link>
      <description>&lt;P&gt;Because I want to do it this way. Dont want to involve vtable in it.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Aug 2017 21:54:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Until-statement-on-decreasing-date/m-p/387998#M93064</guid>
      <dc:creator>ArpitSharma</dc:creator>
      <dc:date>2017-08-14T21:54:57Z</dc:date>
    </item>
    <item>
      <title>Re: Do Until statement on decreasing date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Until-statement-on-decreasing-date/m-p/388000#M93065</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dataset = name_of_sas_dataset;&lt;BR /&gt;proc sql;
select memname into: end_date
from dictionary.tables
where libname="LIB" and memname eqt upcase("&amp;amp;dataset.")
having input(scan(memname,-1,"_"),best.) = max(input(scan(memname,-1,"_"),best.));
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Aug 2017 22:45:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Until-statement-on-decreasing-date/m-p/388000#M93065</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-08-14T22:45:04Z</dc:date>
    </item>
    <item>
      <title>Re: Do Until statement on decreasing date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Until-statement-on-decreasing-date/m-p/388009#M93068</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/325"&gt;@ArpitSharma&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;To query the dictionary tables is the right thing to do.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW for the code you've posted:&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token macroname"&gt;%let&lt;/SPAN&gt; end_date&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token macroname"&gt;%eval&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;end_date&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What do you expect to happen if you start with an end_date like&amp;nbsp;201701? I believe you would first need to convert the string to a SAS date value before any such calculations.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Aug 2017 22:33:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Until-statement-on-decreasing-date/m-p/388009#M93068</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-08-14T22:33:45Z</dc:date>
    </item>
    <item>
      <title>Re: Do Until statement on decreasing date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Until-statement-on-decreasing-date/m-p/388010#M93069</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/325"&gt;@ArpitSharma&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the following code:&lt;/P&gt;
&lt;P&gt;so i want to have the end_date macro variable have the value of the latest dataset.&lt;/P&gt;
&lt;P&gt;How can i have this working?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please suggest.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;And what is is supposed to actually do?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Aug 2017 22:38:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Until-statement-on-decreasing-date/m-p/388010#M93069</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-08-14T22:38:03Z</dc:date>
    </item>
    <item>
      <title>Re: Do Until statement on decreasing date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Until-statement-on-decreasing-date/m-p/388042#M93078</link>
      <description>&lt;P&gt;I know of that part. &amp;nbsp;I actually have a macro that takes care of it. I just used %eval() here for simplicity purpose.The macro that i have will go to 201612 if i have the parameter as 201701.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2017 02:08:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Until-statement-on-decreasing-date/m-p/388042#M93078</guid>
      <dc:creator>ArpitSharma</dc:creator>
      <dc:date>2017-08-15T02:08:25Z</dc:date>
    </item>
    <item>
      <title>Re: Do Until statement on decreasing date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Until-statement-on-decreasing-date/m-p/388048#M93081</link>
      <description>&lt;P&gt;The most obvious problem with this approach is the missunderstanding of what macro code does. &amp;nbsp;Macro code is evaluated BEFORE the code is compiled and run. That is the whole point of macro code, to generate SAS code. &amp;nbsp;So you attempted to run this data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA _NULL_;
   DO UNTIL(fileexist(&amp;amp;path_lib.&amp;amp;dataset._&amp;amp;end_date..sas7bdat));
   END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now that has syntax errors, but the logic error is what is most obvious. There is no way for the value passed to the FILEEXIST() function to ever change.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It will be much easier to code this if you just code it in regular SAS code. You can use the macro variables to make it more flexible, but the basic logic is in normal SAS code. &amp;nbsp;No need to use FILEEXIST() when looking for datasets, use EXISTS() instead. Use the INTNX() function to move by month. Use YYMMN6. format to convert a month value into a YYYYMM string. &amp;nbsp;You should also make sure to have a way to insure that it will finish. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname lib "/sas/dataset/folder/path/";*this is automated;
%let dataset = name_of_sas_dataset_;
%let end_date='01JUL2017'd ;

data _null_;
  found=0;
  do offset=0 to -24 by -1 until(found);
    end_date=intnx('month',&amp;amp;end_date,offset,'b');
    if exist(cats("lib.&amp;amp;dataset",put(end_date,yymmn6.)) then found=1;
  end;
  call symputx('end_date',end_date);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might also want to add more logic to properly set the END_DATE macro when no dataset is found. Or to format the value stored in the macro variable in some other way.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2017 03:33:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Until-statement-on-decreasing-date/m-p/388048#M93081</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-08-15T03:33:26Z</dc:date>
    </item>
  </channel>
</rss>

