<?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: Using INFILE to read csv in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-INFILE-to-read-csv/m-p/948008#M371012</link>
    <description>&lt;P&gt;By latest, I mean the file which was last modified.&lt;/P&gt;</description>
    <pubDate>Fri, 18 Oct 2024 08:26:23 GMT</pubDate>
    <dc:creator>aashi77</dc:creator>
    <dc:date>2024-10-18T08:26:23Z</dc:date>
    <item>
      <title>Using INFILE to read csv</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-INFILE-to-read-csv/m-p/947624#M370924</link>
      <description>&lt;P&gt;In the folder, there are 2 files: a1.csv and a2.csv&lt;BR /&gt;The part of the code is:&lt;/P&gt;&lt;P&gt;infile "/doc/a*.csv";&lt;BR /&gt;It is reading both files. I want only the latest one.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Oct 2024 06:46:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-INFILE-to-read-csv/m-p/947624#M370924</guid>
      <dc:creator>aashi77</dc:creator>
      <dc:date>2024-10-16T06:46:14Z</dc:date>
    </item>
    <item>
      <title>Re: Using INFILE to read csv</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-INFILE-to-read-csv/m-p/947625#M370925</link>
      <description>&lt;P&gt;I assume "latest" means the file with the highest number.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will need to first create a directory listing and then use some logic to find the the file with the highest number (and it's not how they sort alphabetically because a2.csv will sort after a10.csv).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you execute OS commands out of SAS? Run below syntax and let us know if you see in the log XCMD or NOXCMD.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc options option=xcmd;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Oct 2024 07:12:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-INFILE-to-read-csv/m-p/947625#M370925</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-10-16T07:12:06Z</dc:date>
    </item>
    <item>
      <title>Re: Using INFILE to read csv</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-INFILE-to-read-csv/m-p/947645#M370926</link>
      <description>&lt;P&gt;Define "latest". Highest number in the name or latest modification timestamp of the file?&lt;/P&gt;</description>
      <pubDate>Wed, 16 Oct 2024 09:32:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-INFILE-to-read-csv/m-p/947645#M370926</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-10-16T09:32:33Z</dc:date>
    </item>
    <item>
      <title>Re: Using INFILE to read csv</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-INFILE-to-read-csv/m-p/947683#M370937</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/470458"&gt;@aashi77&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;In the folder, there are 2 files: a1.csv and a2.csv&lt;BR /&gt;The part of the code is:&lt;/P&gt;
&lt;P&gt;infile "/doc/a*.csv";&lt;BR /&gt;It is reading both files. I want only the latest one.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Do not use the * wildcard in the filename if you only want one file.&lt;/P&gt;
&lt;P&gt;Which file is the latest one?&amp;nbsp; How do you know (what is the logic you used to pick)?&amp;nbsp; Can you convert that logic into code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is easy to get the list of files in a directory.&amp;nbsp; For example see this macro:&amp;nbsp;&lt;A href="https://github.com/sasutils/macros/blob/master/dirtree.sas" target="_blank" rel="noopener"&gt;https://github.com/sasutils/macros/blob/master/dirtree.sas&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But there are other ways also.&amp;nbsp; You could even use your wildcard trick if you only need the NAME of the file to figure out which is the latest.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data filelist;
  length fname filename $200;
  infile "/doc/a*.csv" filename=fname;
  input;
  if fname ne lag(fname);
  filename=fname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once you have picked the right filename there are many ways to use that name in your step that actually reads the file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could put it into a macro variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set filelist;
  call symputx('filename',filename);
run;
data want;
  infile "&amp;amp;filename" dsd truncover firstobs=2;
... rest of step to read CSV file...
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or skip the macro variable and just leave the name in a dataset and use the FILEVAR= option of INFILE statement to tell SAS which file to read.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if _n_=1 then set filelist;
  infile dummy filevar=filename dsd truncover firstobs=2;
... rest of step to read CSV file...
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Oct 2024 14:26:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-INFILE-to-read-csv/m-p/947683#M370937</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-16T14:26:24Z</dc:date>
    </item>
    <item>
      <title>Re: Using INFILE to read csv</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-INFILE-to-read-csv/m-p/948008#M371012</link>
      <description>&lt;P&gt;By latest, I mean the file which was last modified.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2024 08:26:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-INFILE-to-read-csv/m-p/948008#M371012</guid>
      <dc:creator>aashi77</dc:creator>
      <dc:date>2024-10-18T08:26:23Z</dc:date>
    </item>
  </channel>
</rss>

