<?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 Excel Files in a folder in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Importing-Excel-Files-in-a-folder/m-p/718241#M27580</link>
    <description>&lt;P&gt;The method described by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;is exactly what you need when running SAS from a PC SAS installation. But if you are working in a company, often you will have a SAS client-server setup, where you use Enterprise Guide or SAS Studio to write and submit code that actually runs on a SAS compute server located on a different machine. Setting the SAS invocation option NOXCMD is the default in those situations, and you won't be able to use FILENAME PIPE, X, SYSEXEC, or other code that executes arbitrary O/S commands on the compute server.&amp;nbsp; And most system administrators will be more comfortable leaving that setting as-is.&lt;/P&gt;
&lt;P&gt;To work with file system files and directories in those cases, consider using the base SAS functions designed for that (FILENAME, DOPEN, DREAD, DCLOSE, FOPEN, FOPTNAME, FREAD, FCLOSE, etc.). These can be executed even with NOXCMD in effect, and as an added bonus, don't require you to know what O/S underlies your SAS session or to be familiar with DOS or Linux syntax.&amp;nbsp;You can experiment with those yourself, or you can try out the macros I personally use for jobs like this. I've shared these macros on Github (&lt;A href="https://github.com/SASJedi/sas-macros" target="_blank"&gt;https://github.com/SASJedi/sas-macros&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;If your SAS can access internet assets, you can use this SAS code to compile the macros in your SAS session:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Get the necessary macro files rom GitHub */
filename getmacro url "https://raw.githubusercontent.com/SASJedi/sas-macros/master/findfiles.sas";
%include getmacro;
filename m1 url "https://raw.githubusercontent.com/SASJedi/sas-macros/master/exist.sas";
%include getmacro;
filename getmacro url "https://raw.githubusercontent.com/SASJedi/sas-macros/master/translate.sas";
%include getmacro;
filename getmacro url "https://raw.githubusercontent.com/SASJedi/sas-macros/master/fileattribs.sas";
%include getmacro;
filename getmacro;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Once you have the macros, you can ask for syntax help using a question mark, like this:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%findFiles(?)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And to use this macro to solve the problem you presented, I'd do something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Put the path to your Excel file folder here: */

%let path=    ;
%findFiles(&amp;amp;path,xls,work.ExcelFiles)

proc sql noprint;
select filename 
   into:thisfile 
   from work.ExcelFiles
   where lowcase(filename) like 'xlfile.%'
   order by input(substr(filename,8,10),mmddyy10.) desc
;
quit;

PROC IMPORT OUT=WANT
            DATAFILE= "&amp;amp;path\&amp;amp;thisfile" 
            DBMS=EXCEL REPLACE;
RUN;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;May the SAS be with you!&lt;/P&gt;
&lt;P&gt;Mark&lt;/P&gt;</description>
    <pubDate>Wed, 10 Feb 2021 14:11:10 GMT</pubDate>
    <dc:creator>SASJedi</dc:creator>
    <dc:date>2021-02-10T14:11:10Z</dc:date>
    <item>
      <title>Importing Excel Files in a folder</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Importing-Excel-Files-in-a-folder/m-p/717646#M27537</link>
      <description>&lt;P&gt;I have Excel Files in a folder as shown below. I need a sas code that will go into the folder and import the latest file with name starting "xlfile". In other words I would like to import only "xlfile.01.05.2021.xls" from the list of files. This code will be run everyday so it should import the latest file each day.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;xlfile.01.02.2021.xls&lt;/P&gt;&lt;P&gt;xlfile.01.03.2021.xls&lt;/P&gt;&lt;P&gt;xlfile.01.04.2021.xls&lt;/P&gt;&lt;P&gt;xlfile.01.05.2021.xls&lt;/P&gt;&lt;P&gt;otherfiles1.xls&lt;/P&gt;&lt;P&gt;otherfiles2.xls&lt;/P&gt;&lt;P&gt;etc&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to a code that will go and import only the latest&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2021 16:48:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Importing-Excel-Files-in-a-folder/m-p/717646#M27537</guid>
      <dc:creator>RADAGBE9</dc:creator>
      <dc:date>2021-02-08T16:48:45Z</dc:date>
    </item>
    <item>
      <title>Re: Importing Excel Files in a folder</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Importing-Excel-Files-in-a-folder/m-p/717664#M27539</link>
      <description>&lt;P&gt;So you need a method to get the list of files in the directory.&amp;nbsp; Then a method to filter to the names of interest. Then a way to tease out the date from the filename. Then a way to find the latest (largest date).&amp;nbsp; And finally a way to import the selected file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The easiest way to do the first three is if you can use operating system commands.&amp;nbsp; So DIR (on Windows) or ls (on unix) will return a list of files.&amp;nbsp; So on Unix you might do something like this (assuming 01.05 means January fifth).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let path=/name/or/your/directory;
data files ;
  infile "cd &amp;amp;path ; ls xlfile.*.xls" pipe truncover ;
  input filename $256. ;
  date = input(substr(filename,8,mmddyy10.);
  format date yymmdd10.;
run;
  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now just sort by the new DATE variable and take the last name and put it into a macro variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=files;
  by date;
run;
data _null_;
  set files end=eof;
  if eof then call symputx('filename',filename);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you can use the macro variable in whatever code you were using to IMPORT the XLS file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc import out=want replace 
  datafile="&amp;amp;path/&amp;amp;filename" dbms=xls
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2021 17:29:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Importing-Excel-Files-in-a-folder/m-p/717664#M27539</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-08T17:29:12Z</dc:date>
    </item>
    <item>
      <title>Re: Importing Excel Files in a folder</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Importing-Excel-Files-in-a-folder/m-p/718241#M27580</link>
      <description>&lt;P&gt;The method described by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;is exactly what you need when running SAS from a PC SAS installation. But if you are working in a company, often you will have a SAS client-server setup, where you use Enterprise Guide or SAS Studio to write and submit code that actually runs on a SAS compute server located on a different machine. Setting the SAS invocation option NOXCMD is the default in those situations, and you won't be able to use FILENAME PIPE, X, SYSEXEC, or other code that executes arbitrary O/S commands on the compute server.&amp;nbsp; And most system administrators will be more comfortable leaving that setting as-is.&lt;/P&gt;
&lt;P&gt;To work with file system files and directories in those cases, consider using the base SAS functions designed for that (FILENAME, DOPEN, DREAD, DCLOSE, FOPEN, FOPTNAME, FREAD, FCLOSE, etc.). These can be executed even with NOXCMD in effect, and as an added bonus, don't require you to know what O/S underlies your SAS session or to be familiar with DOS or Linux syntax.&amp;nbsp;You can experiment with those yourself, or you can try out the macros I personally use for jobs like this. I've shared these macros on Github (&lt;A href="https://github.com/SASJedi/sas-macros" target="_blank"&gt;https://github.com/SASJedi/sas-macros&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;If your SAS can access internet assets, you can use this SAS code to compile the macros in your SAS session:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Get the necessary macro files rom GitHub */
filename getmacro url "https://raw.githubusercontent.com/SASJedi/sas-macros/master/findfiles.sas";
%include getmacro;
filename m1 url "https://raw.githubusercontent.com/SASJedi/sas-macros/master/exist.sas";
%include getmacro;
filename getmacro url "https://raw.githubusercontent.com/SASJedi/sas-macros/master/translate.sas";
%include getmacro;
filename getmacro url "https://raw.githubusercontent.com/SASJedi/sas-macros/master/fileattribs.sas";
%include getmacro;
filename getmacro;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Once you have the macros, you can ask for syntax help using a question mark, like this:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%findFiles(?)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And to use this macro to solve the problem you presented, I'd do something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Put the path to your Excel file folder here: */

%let path=    ;
%findFiles(&amp;amp;path,xls,work.ExcelFiles)

proc sql noprint;
select filename 
   into:thisfile 
   from work.ExcelFiles
   where lowcase(filename) like 'xlfile.%'
   order by input(substr(filename,8,10),mmddyy10.) desc
;
quit;

PROC IMPORT OUT=WANT
            DATAFILE= "&amp;amp;path\&amp;amp;thisfile" 
            DBMS=EXCEL REPLACE;
RUN;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;May the SAS be with you!&lt;/P&gt;
&lt;P&gt;Mark&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2021 14:11:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Importing-Excel-Files-in-a-folder/m-p/718241#M27580</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2021-02-10T14:11:10Z</dc:date>
    </item>
  </channel>
</rss>

