<?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: Importing a File with the most recent Date Modified in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612513#M178753</link>
    <description>&lt;P&gt;Talk to your SAS administrator about enabling use of OS commands from SAS. By default this is switched off in server installations.&lt;/P&gt;</description>
    <pubDate>Tue, 17 Dec 2019 19:06:06 GMT</pubDate>
    <dc:creator>SASKiwi</dc:creator>
    <dc:date>2019-12-17T19:06:06Z</dc:date>
    <item>
      <title>Importing a File with the most recent Date Modified</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612453#M178718</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using Base SAS 9.4.&amp;nbsp; I'm trying to import an Excel file into a SAS data set (which I know how to do).&amp;nbsp; The problem is, there are hundreds of files in a folder and I need to import the file with the most recent Date Modified.&amp;nbsp; If it helps at all, every file follows the same naming convention.&amp;nbsp; Example:&amp;nbsp; FileABCD_123456.&amp;nbsp; The file with the maximum value of the six digits following the underscore is the file I want to import.&amp;nbsp; Can anyone help?&amp;nbsp; Much appreciated!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-Adam&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2019 16:50:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612453#M178718</guid>
      <dc:creator>awmeyertimmy</dc:creator>
      <dc:date>2019-12-17T16:50:34Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a File with the most recent Date Modified</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612478#M178727</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/59133"&gt;@awmeyertimmy&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can try the following code to retrieve the number of the file in the macrovariable &amp;amp;num_file&lt;/P&gt;
&lt;P&gt;eg.&amp;nbsp;&amp;amp;num_file will take the value&amp;nbsp;&lt;SPAN&gt;123456 in your example, if 123456 is the maximum value among several numbers.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;NB: this code assumes the number does not exceed 8 digits.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
	select max(input(prxchange('s/^.+_//',1,trim(memname)),8.))
	into: num_file trimmed
	from dictionary.tables
	where libname = "WORK" and prxmatch('/_\d+$/',trim(memname));
quit;

proc sql;
	select memname
	from dictionary.tables
	where libname = "WORK" and scan(trim(memname),2,"_")="&amp;amp;num_file";
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;NB: please adapt your libname in the WHERE clause.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2019 17:53:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612478#M178727</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-17T17:53:38Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a File with the most recent Date Modified</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612494#M178738</link>
      <description>&lt;P&gt;Get the list of files. Parse out that numeric suffix. Find the maximum suffix.&amp;nbsp; Put the name into a macro variable. Use the macro variable in place of the hard coded name in the code to read one Excel file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first part is easier if you know what type of operating system SAS is running on and you are allowed to run operating system commands.&amp;nbsp; Otherwise you will need to resort to use the DREAD() function. (check the on-documentation for an example).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming you are running on Windows you would just use the DIR command to get the list of files.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let path=\\servername\sharename\foldername;
data files:
  infile "dir &amp;amp;path\*.xlsx /b" pipe truncover ;
  input filename $256. ;
  length suffix $32 number 8;
  suffix = scan(filename,-2,'._');
  number = input(suffix,??32.);
run;

proc sql noprint;
  select filename into :fname trimmed
  from files where number = max(number)
  ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now just use "&amp;amp;path/&amp;amp;fname" as the name of the xlsx file to convert.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2019 18:26:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612494#M178738</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-12-17T18:26:48Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a File with the most recent Date Modified</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612500#M178744</link>
      <description>&lt;P&gt;I'm sorry, yes I am running Windows.&amp;nbsp; When I run that code it says that I have insufficient authorization to access PIPE.&amp;nbsp; So unfortunately, that isn't working for me.&amp;nbsp; Thank you though!&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2019 18:42:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612500#M178744</guid>
      <dc:creator>awmeyertimmy</dc:creator>
      <dc:date>2019-12-17T18:42:07Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a File with the most recent Date Modified</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612513#M178753</link>
      <description>&lt;P&gt;Talk to your SAS administrator about enabling use of OS commands from SAS. By default this is switched off in server installations.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2019 19:06:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612513#M178753</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2019-12-17T19:06:06Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a File with the most recent Date Modified</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612520#M178757</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292097"&gt;@ed_sas_member&lt;/a&gt;&amp;nbsp; does that "dictionary.files" work if the files are csv files or do they need to be sas data sets.&amp;nbsp; The reason I ask is that I am getting no observations (however the code executes successfully)&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2019 19:59:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612520#M178757</guid>
      <dc:creator>awmeyertimmy</dc:creator>
      <dc:date>2019-12-17T19:59:08Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a File with the most recent Date Modified</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612523#M178760</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/59133"&gt;@awmeyertimmy&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am sorry, because I just realize that I made a mistake when I read your question: I thought you talked about SAS datasets and not other kind of file like csv.&lt;/P&gt;
&lt;P&gt;To answer your question, the dictionary.tables table stores a lot of information regarding SAS datasets and is accessible via proc sql.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2019 20:05:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612523#M178760</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-17T20:05:42Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a File with the most recent Date Modified</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612525#M178761</link>
      <description>&lt;P&gt;Then you will have resort to a slightly more complex solution to get the list of files.&lt;/P&gt;
&lt;P&gt;Make a fileref pointing to the directory. Open the directory with DOPEN(), And then read the names using DREAD().&amp;nbsp; You will need to add a test for whether the file has XLSX extension on its name or not.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let path=\\servername\sharename\foldername;
 
data files;
  length filename $256 suffix $32 number 8;
  keep filename suffix number;
  length rc did index 8 fileref $8 ;
  rc=filename(fileref,"&amp;amp;path");
  did=dopen(fileref);
  do index=1 to dnum(did);
    filename=dread(did,index);
    if upcase(scan(filename,-1,'.')) = 'XLSX' then do;
      suffix = scan(filename,-2,'._');
      number = input(suffix,??32.);
      output;
    end;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2019 20:16:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612525#M178761</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-12-17T20:16:55Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a File with the most recent Date Modified</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612527#M178762</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/59133"&gt;@awmeyertimmy&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;please have a look at&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;‘s answer (like below) to retrieve file names in a SAS dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/* untested code */&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="bbcode_container"&gt;
&lt;TABLE cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD valign="top"&gt;
&lt;PRE&gt;&lt;SPAN&gt;data&lt;/SPAN&gt; files &lt;SPAN class="br0"&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;keep&lt;/SPAN&gt;=Memname&lt;SPAN class="br0"&gt;)&lt;/SPAN&gt;;
&lt;SPAN&gt;length&lt;/SPAN&gt; memname $&lt;SPAN&gt;256&lt;/SPAN&gt;;
	fich=&lt;SPAN&gt;filename&lt;/SPAN&gt;&lt;SPAN class="br0"&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;'fich'&lt;/SPAN&gt;,&lt;SPAN&gt;"&amp;amp;path"&lt;/SPAN&gt;&lt;SPAN class="br0"&gt;)&lt;/SPAN&gt;;
	did=&lt;SPAN&gt;dopen&lt;/SPAN&gt;&lt;SPAN class="br0"&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;'fich'&lt;/SPAN&gt;&lt;SPAN class="br0"&gt;)&lt;/SPAN&gt;;
	nb_fich=&lt;SPAN&gt;dnum&lt;/SPAN&gt;&lt;SPAN class="br0"&gt;(&lt;/SPAN&gt;did&lt;SPAN class="br0"&gt;)&lt;/SPAN&gt;;
	&lt;SPAN&gt;do&lt;/SPAN&gt; i=&lt;SPAN&gt;1&lt;/SPAN&gt; TO nb_fich;
		memname=&lt;SPAN&gt;dread&lt;/SPAN&gt;&lt;SPAN class="br0"&gt;(&lt;/SPAN&gt;did,i&lt;SPAN class="br0"&gt;)&lt;/SPAN&gt;;
		&lt;SPAN&gt;output&lt;/SPAN&gt;;
	&lt;SPAN&gt;end&lt;/SPAN&gt;;
	rc=&lt;SPAN&gt;dclose&lt;/SPAN&gt;&lt;SPAN class="br0"&gt;(&lt;/SPAN&gt;did&lt;SPAN class="br0"&gt;)&lt;/SPAN&gt;;
&lt;SPAN&gt;run&lt;/SPAN&gt;;&lt;/PRE&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;</description>
      <pubDate>Tue, 17 Dec 2019 20:47:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612527#M178762</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-17T20:47:26Z</dc:date>
    </item>
    <item>
      <title>Re: Importing a File with the most recent Date Modified</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612540#M178764</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292097"&gt;@ed_sas_member&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13976"&gt;@SASKiwi&lt;/a&gt;&amp;nbsp;Thanks everyone for your help! Much appreciated!&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2019 21:14:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-a-File-with-the-most-recent-Date-Modified/m-p/612540#M178764</guid>
      <dc:creator>awmeyertimmy</dc:creator>
      <dc:date>2019-12-17T21:14:25Z</dc:date>
    </item>
  </channel>
</rss>

