<?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: Read Latest File From A Folder in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Read-Latest-File-From-A-Folder/m-p/477465#M122963</link>
    <description>&lt;P&gt;You can make your life easier by orders of magnitude by using an intelligent format for the dates. MMDDYYYY is dumb, YYYYMMDD (as used in the ISO 8601 standard) is intelligent.&lt;/P&gt;
&lt;P&gt;I'd then get the last file by doing that (UNIX):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename oscmd pipe "ls &amp;amp;path./Sales_&amp;amp;month.&amp;amp;year._*.txt|tail -1";

data _null_;
infile oscmd truncover;
input filename $100.;
call symput('infile',filename);
run;

data want;
infile "&amp;amp;infile" .........&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;since UNIX ls orders automatically.&lt;/P&gt;
&lt;P&gt;With your filenames, you need to work harder:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename oscmd pipe "ls &amp;amp;path./Sales_&amp;amp;month.&amp;amp;year._*.txt";

data files;
infile oscmd truncover;
input filename $100.;
date = input(scan(scan(scan(filename,-1,'/'),1,'.'),3,'_'),mmddyy8.);
run;

proc sort data=files;
by descending date;
run;

data _null_;
set files;
call symput('infile',filename);
stop; *get only first obs;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="1 2 3 4 5 6 7"&gt;&lt;EM&gt;Edit: added the pipe engine in the filename statements.&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 12 Jul 2018 13:42:57 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-07-12T13:42:57Z</dc:date>
    <item>
      <title>Read Latest File From A Folder</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-Latest-File-From-A-Folder/m-p/477453#M122958</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First off, I'm a Tableau professional finding his foot in SAS world. So if my questions are naive, I apologize in advance to the group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've a requirement where I have to read latest file from last from a folder. So the requirements is something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Requirements:&lt;/P&gt;&lt;P&gt;1. Folder A stores files for sales as:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Sales_062018_06102018.txt&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;Sales_062018_06132018.txt&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Sales_062018_06152018.txt&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Sales_062018_06182018.txt&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2. I've to go to the folder and pick the latest file (In this case&amp;nbsp;Sales_062018_06182018.txt)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3. Any suggestions on how I should go and read the files and get the latest file? The number of files in the folder can be more than four.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Any pseudo code or code structure would be very helpful. Thank you&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 13:25:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-Latest-File-From-A-Folder/m-p/477453#M122958</guid>
      <dc:creator>SteelersPitts</dc:creator>
      <dc:date>2018-07-12T13:25:09Z</dc:date>
    </item>
    <item>
      <title>Re: Read Latest File From A Folder</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-Latest-File-From-A-Folder/m-p/477465#M122963</link>
      <description>&lt;P&gt;You can make your life easier by orders of magnitude by using an intelligent format for the dates. MMDDYYYY is dumb, YYYYMMDD (as used in the ISO 8601 standard) is intelligent.&lt;/P&gt;
&lt;P&gt;I'd then get the last file by doing that (UNIX):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename oscmd pipe "ls &amp;amp;path./Sales_&amp;amp;month.&amp;amp;year._*.txt|tail -1";

data _null_;
infile oscmd truncover;
input filename $100.;
call symput('infile',filename);
run;

data want;
infile "&amp;amp;infile" .........&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;since UNIX ls orders automatically.&lt;/P&gt;
&lt;P&gt;With your filenames, you need to work harder:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename oscmd pipe "ls &amp;amp;path./Sales_&amp;amp;month.&amp;amp;year._*.txt";

data files;
infile oscmd truncover;
input filename $100.;
date = input(scan(scan(scan(filename,-1,'/'),1,'.'),3,'_'),mmddyy8.);
run;

proc sort data=files;
by descending date;
run;

data _null_;
set files;
call symput('infile',filename);
stop; *get only first obs;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="1 2 3 4 5 6 7"&gt;&lt;EM&gt;Edit: added the pipe engine in the filename statements.&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 13:42:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-Latest-File-From-A-Folder/m-p/477465#M122963</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-07-12T13:42:57Z</dc:date>
    </item>
    <item>
      <title>Re: Read Latest File From A Folder</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-Latest-File-From-A-Folder/m-p/477467#M122965</link>
      <description>&lt;P&gt;There are a lot of examples here in the SAS Communities of code to read the names of files in a specific folder. This is one such example:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/how-to-read-filenames-of-a-directory-into-a-file/td-p/71496" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/how-to-read-filenames-of-a-directory-into-a-file/td-p/71496&lt;/A&gt;. Once you have read the file names into a data set, then finding the one with the biggest date and time in the file name is simple.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Searching via your favorite search engine will find many more examples.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 13:42:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-Latest-File-From-A-Folder/m-p/477467#M122965</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-07-12T13:42:59Z</dc:date>
    </item>
    <item>
      <title>Re: Read Latest File From A Folder</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-Latest-File-From-A-Folder/m-p/477476#M122974</link>
      <description>&lt;P&gt;Thank you for your valuable feedback. I appreciate it. I will code and let you know how it went.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 13:56:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-Latest-File-From-A-Folder/m-p/477476#M122974</guid>
      <dc:creator>SteelersPitts</dc:creator>
      <dc:date>2018-07-12T13:56:19Z</dc:date>
    </item>
    <item>
      <title>Re: Read Latest File From A Folder</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-Latest-File-From-A-Folder/m-p/477477#M122975</link>
      <description>&lt;P&gt;Thank you. Appreciate your feedback.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 13:56:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-Latest-File-From-A-Folder/m-p/477477#M122975</guid>
      <dc:creator>SteelersPitts</dc:creator>
      <dc:date>2018-07-12T13:56:44Z</dc:date>
    </item>
  </channel>
</rss>

