<?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 Retrieve and clean all files in a directory one by one in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-and-clean-all-files-in-a-directory-one-by-one/m-p/827202#M326740</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am running a SAS program on a cloud (which I believe is a unix/linux system).&amp;nbsp; Basically, I want to import and clean every cvs file in a directory.&amp;nbsp; All the files start with log. I find this support working in my local computer:&amp;nbsp;&lt;A href="http://support.sas.com/kb/41/880.html" target="_blank"&gt;http://support.sas.com/kb/41/880.html&lt;/A&gt;&amp;nbsp; &amp;nbsp;However, when I run it in the cloud, it does not work.&amp;nbsp; My code in the cloud is as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename DIRLIST pipe 'dir "/scratch/dg/log/SAS/log*.csv" /b ';

data dirlist ;
   infile dirlist lrecl=200 truncover;
   input file_name $100.;
run;

data edgar.dirlist;set dirlist;run;

data _null_;
   set dirlist end=end;
   count+1;
   call symputx('read'||put(count,4.-l),cats('/scratch/dg/log/SAS/',file_name));
   call symputx('dset'||put(count,4.-l),scan(file_name,1,'.'));
   if end then call symputx('max',count);
run;

options mprint symbolgen;
%macro readin;
   %do i=1 %to &amp;amp;max;

 
data seclog;
infile "&amp;amp;&amp;amp;read&amp;amp;i" delimiter = ',' MISSOVER
DSD lrecl=32767 firstobs=2 ;
informat ip $15. ;
informat date yymmdd10. ;
informat time anydtdtm40. ;


format ip $15. ;
format date yymmdd10. ;
format time datetime. ;

input
ip $ date time ;
run;


   %end;
%mend readin;

%readin;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The error message is as follow:&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MPRINT(READIN):   infile "/scratch/dg/log/SAS/dir: cannot access '/scratch/dg/log/SAS/log*.csv': No such file 
or directory" delimiter = ',' MISSOVER DSD lrecl=32767 firstobs=2 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;When I opened the dirlist and I did find that there are two variables:&amp;nbsp; The variable name is file_name.&amp;nbsp; But the value of the variable is&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dir: cannot access '/scratch/dg/log/SAS/log*.csv': No such file or directory&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would appreciate it very much if someone can help here.&lt;/P&gt;</description>
    <pubDate>Thu, 04 Aug 2022 18:44:15 GMT</pubDate>
    <dc:creator>daradanye</dc:creator>
    <dc:date>2022-08-04T18:44:15Z</dc:date>
    <item>
      <title>Retrieve and clean all files in a directory one by one</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-and-clean-all-files-in-a-directory-one-by-one/m-p/827202#M326740</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am running a SAS program on a cloud (which I believe is a unix/linux system).&amp;nbsp; Basically, I want to import and clean every cvs file in a directory.&amp;nbsp; All the files start with log. I find this support working in my local computer:&amp;nbsp;&lt;A href="http://support.sas.com/kb/41/880.html" target="_blank"&gt;http://support.sas.com/kb/41/880.html&lt;/A&gt;&amp;nbsp; &amp;nbsp;However, when I run it in the cloud, it does not work.&amp;nbsp; My code in the cloud is as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename DIRLIST pipe 'dir "/scratch/dg/log/SAS/log*.csv" /b ';

data dirlist ;
   infile dirlist lrecl=200 truncover;
   input file_name $100.;
run;

data edgar.dirlist;set dirlist;run;

data _null_;
   set dirlist end=end;
   count+1;
   call symputx('read'||put(count,4.-l),cats('/scratch/dg/log/SAS/',file_name));
   call symputx('dset'||put(count,4.-l),scan(file_name,1,'.'));
   if end then call symputx('max',count);
run;

options mprint symbolgen;
%macro readin;
   %do i=1 %to &amp;amp;max;

 
data seclog;
infile "&amp;amp;&amp;amp;read&amp;amp;i" delimiter = ',' MISSOVER
DSD lrecl=32767 firstobs=2 ;
informat ip $15. ;
informat date yymmdd10. ;
informat time anydtdtm40. ;


format ip $15. ;
format date yymmdd10. ;
format time datetime. ;

input
ip $ date time ;
run;


   %end;
%mend readin;

%readin;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The error message is as follow:&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MPRINT(READIN):   infile "/scratch/dg/log/SAS/dir: cannot access '/scratch/dg/log/SAS/log*.csv': No such file 
or directory" delimiter = ',' MISSOVER DSD lrecl=32767 firstobs=2 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;When I opened the dirlist and I did find that there are two variables:&amp;nbsp; The variable name is file_name.&amp;nbsp; But the value of the variable is&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dir: cannot access '/scratch/dg/log/SAS/log*.csv': No such file or directory&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would appreciate it very much if someone can help here.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Aug 2022 18:44:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieve-and-clean-all-files-in-a-directory-one-by-one/m-p/827202#M326740</guid>
      <dc:creator>daradanye</dc:creator>
      <dc:date>2022-08-04T18:44:15Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieve and clean all files in a directory one by one</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-and-clean-all-files-in-a-directory-one-by-one/m-p/827204#M326741</link>
      <description>&lt;P&gt;If the "/scratch/" folder is not a root for the path you need to provide one.&lt;/P&gt;
&lt;P&gt;The Path has to be as the computer running the DIR command sees it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would suggest instead a data _null_ and a bunch of Call Symputx in this step&lt;/P&gt;
&lt;PRE&gt;data _null_;
   set dirlist end=end;
   count+1;
   call symputx('read'||put(count,4.-l),cats('/scratch/dg/log/SAS/',file_name));
   call symputx('dset'||put(count,4.-l),scan(file_name,1,'.'));
   if end then call symputx('max',count);
run;
&lt;/PRE&gt;
&lt;P&gt;to assign the value of the Put(count,4. -l) etc to actual variables. Then look the values of those variables. You might just find some odd values depending of which version of DIR is involve.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Aug 2022 18:53:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieve-and-clean-all-files-in-a-directory-one-by-one/m-p/827204#M326741</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-08-04T18:53:07Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieve and clean all files in a directory one by one</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-and-clean-all-files-in-a-directory-one-by-one/m-p/827206#M326742</link>
      <description>&lt;P&gt;The /scratch/ is the root for the path.&amp;nbsp; I have several steps before to import data from a folder in scratch and it works.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Aug 2022 19:02:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieve-and-clean-all-files-in-a-directory-one-by-one/m-p/827206#M326742</guid>
      <dc:creator>daradanye</dc:creator>
      <dc:date>2022-08-04T19:02:56Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieve and clean all files in a directory one by one</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-and-clean-all-files-in-a-directory-one-by-one/m-p/827210#M326745</link>
      <description>&lt;P&gt;Run this for a test:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data files;
length dref $8 name $200;
rc = filename(dref,"/scratch/dg/log/SAS");
did = dopen(dref);
if did
then do;
  do i = 1 to dnum(did);
    name = dread(did,i);
    output;
  end;
  rc = dclose(did);
end;
else putlog "Directory can't be opened";
rc = filename(dref);
keep name;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;See which names, if any, you get, or if you get the error message in the log.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Aug 2022 19:19:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieve-and-clean-all-files-in-a-directory-one-by-one/m-p/827210#M326745</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-08-04T19:19:35Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieve and clean all files in a directory one by one</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-and-clean-all-files-in-a-directory-one-by-one/m-p/827211#M326746</link>
      <description>&lt;P&gt;DIR is a Windows command, not Unix.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you have pipe access?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Filename filelist pipe "ls  /scratch/dg/log/SAS/log*.csv "; 
                                                                                   
   Data dirList;                                        
     Infile filelist truncover;
     Input filename $100.;
   Run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, if all the files have the same layout and you want a single file at the end this is much easier.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sec_log;
 
*make sure variables to store file name are long enough;
length filename txt_file_name $256;
 informat ip $15. ;
informat date yymmdd10. ;
informat time anydtdtm40. ;


format ip $15. ;
format date yymmdd10. ;
format time datetime. ;


*keep file name from record to record;
retain txt_file_name;
 
*Use wildcard in input;
infile "'/scratch/dg/log/SAS/log*.csv' " eov=eov filename=filename truncover;
 
*Input first record and hold line;
input@;
 
*Check if this is the first record or the first record in a new file;
*If it is, replace the filename with the new file name and move to next line;
if _n_ eq 1 or eov then do;
txt_file_name = scan(filename, -1, "/");
eov=0;
delete;
end;
 
*Otherwise  go to the import step and read the files;
else input ip $ date time ;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 04 Aug 2022 19:20:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieve-and-clean-all-files-in-a-directory-one-by-one/m-p/827211#M326746</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-08-04T19:20:14Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieve and clean all files in a directory one by one</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-and-clean-all-files-in-a-directory-one-by-one/m-p/827216#M326748</link>
      <description>&lt;P&gt;Sounds like the directory you are trying to search is not on the machine where your SAS code is running.&lt;/P&gt;
&lt;P&gt;Also DIR is not really a Unix command, although a lot of unix version have implemented something like it, but it does not support the Windows style /b option.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&amp;gt;dir test/*log /b
dir: cannot access /b: No such file or directory
test/aaabatch_test1.log                test/endsas.log              test/m6.log
test/sasver.log        test/where_in.log
&lt;/PRE&gt;
&lt;P&gt;Here is what you get if the directory does not exist (or you cannot read from it).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;gt;dir /scratch/nosuchdir/*.log /b
dir: cannot access /scratch/nosuchdir/*.log: No such file or directory
dir: cannot access /b: No such file or directory
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So take two immediate steps.&lt;/P&gt;
&lt;P&gt;1) Remove the /b&lt;/P&gt;
&lt;P&gt;2) Figure out whether the directory you are trying to read the files from is actually available on the machine where SAS is running.&amp;nbsp; And if it is what its actual name is.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Aug 2022 19:48:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieve-and-clean-all-files-in-a-directory-one-by-one/m-p/827216#M326748</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-04T19:48:53Z</dc:date>
    </item>
  </channel>
</rss>

