<?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: Loop through csv files and import in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/706901#M217050</link>
    <description>All files have same structures except that they are generated on different days.</description>
    <pubDate>Fri, 18 Dec 2020 08:24:18 GMT</pubDate>
    <dc:creator>JLin2021</dc:creator>
    <dc:date>2020-12-18T08:24:18Z</dc:date>
    <item>
      <title>Loop through csv files and import</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/706890#M217046</link>
      <description>&lt;P&gt;Hi all&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to edit a macro which can import all csv files in a directory into SAS.&lt;/P&gt;&lt;P&gt;However, it seems there is error if a csv file contains no data (i.e. only header row).&lt;/P&gt;&lt;P&gt;Is there any way to ignore the error? Or to avoid importing those csv files? Thanks!&lt;/P&gt;&lt;PRE&gt;%macro importfile(location);
filename _dir_ "%bquote(&amp;amp;location.)";
data _null_;
handle = dopen('_dir_');
if handle &amp;gt; 0 then do;
count = dnum(handle);
do i = 1 to count;

if find(dread(handle,i),'csv') &amp;gt; 0 then call execute(cat('Proc Import Out = Work.Testing', i, " DATAFILE= "', &amp;amp;location., dread(handle,i), '"DBMS = csv replace; run;'));
end;
end;
rc=dclose(handle);
run;
filename _dir_ clear;
%mend;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Dec 2020 06:40:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/706890#M217046</guid>
      <dc:creator>JLin2021</dc:creator>
      <dc:date>2020-12-18T06:40:50Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through csv files and import</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/706897#M217048</link>
      <description>&lt;P&gt;I don't see an easy way to solve the problem, except for replacing proc import by a data step. But this is, of course, hardly possible in your current setup. Do all files have the same structure?&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2020 07:47:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/706897#M217048</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-12-18T07:47:51Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through csv files and import</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/706900#M217049</link>
      <description>&lt;P&gt;Thanks for checking.&lt;/P&gt;&lt;P&gt;I searched that there is a macro which can be used to check whether the file has data or not (&lt;A href="https://stackoverflow.com/questions/61742478/importing-empty-csv-file-in-sas" target="_blank"&gt;https://stackoverflow.com/questions/61742478/importing-empty-csv-file-in-sas&lt;/A&gt;)&lt;/P&gt;&lt;P&gt;But then I modify it a bit and it returns error. It only works if I change CSVName to full path&amp;amp;filename, i.e. C:\testing\testing.csv.&amp;nbsp;&lt;/P&gt;&lt;P&gt;If this macro works, then I can replace my call execute proc import statement by calling this macro:&lt;/P&gt;&lt;PRE&gt;%macro CheckCSV(CSVName);
data _null_;
if _N_ &amp;gt; 2 or eof then do;
call symputx('Flag', _n_ -1);
stop;
end;
infile "C:\testing\&amp;amp;CSVName" end=eof;
input;
run;

%if &amp;amp;Flag &amp;gt; 1 %then %do;
proc import out = Work.Testing Datafile = "C:\testing\&amp;amp;CSVName" dbms = csv replace;
run;
%end;
%mend;&lt;BR /&gt;&lt;BR /&gt;%CheckCSV("testing.csv")&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Dec 2020 08:22:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/706900#M217049</guid>
      <dc:creator>JLin2021</dc:creator>
      <dc:date>2020-12-18T08:22:57Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through csv files and import</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/706901#M217050</link>
      <description>All files have same structures except that they are generated on different days.</description>
      <pubDate>Fri, 18 Dec 2020 08:24:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/706901#M217050</guid>
      <dc:creator>JLin2021</dc:creator>
      <dc:date>2020-12-18T08:24:18Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through csv files and import</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/706904#M217051</link>
      <description>&lt;P&gt;Then you use the data step, and a wildcard in the INFILE statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
length fname infilename $200;
infile "/path/*.csv" dlm=',' dsd truncover filename=fname;
infilename = fname; /* puts the current filename into the dataset */
input
  ....
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Dec 2020 08:39:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/706904#M217051</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-12-18T08:39:49Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through csv files and import</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/706947#M217058</link>
      <description>Thanks a lot! I tried your code and the dataset created contains a no. of duplicated values (all values are full filepath&amp;amp;filename) for those files with data.&lt;BR /&gt;Then I create another dataset with distinct values and then loop through each value to execute proc import. Is there smarter way to do?&lt;BR /&gt;Sorry I'm new to SAS...&lt;BR /&gt;And is there any reason why the CheckCSV macro is not working?</description>
      <pubDate>Fri, 18 Dec 2020 14:06:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/706947#M217058</guid>
      <dc:creator>JLin2021</dc:creator>
      <dc:date>2020-12-18T14:06:56Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through csv files and import</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/706998#M217062</link>
      <description>&lt;P&gt;You need to combine this macro with your other code that is finding the list of file names.&lt;/P&gt;
&lt;P&gt;First I would fix the macro a little. Use a more accurate name and pass in the target dataset name along with the CSV filename.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro importcsv(CSVName,DSName);
%local flag ;
data _null_;
  call symputx('Flag', _n_ -1);
  infile "&amp;amp;CSVName";
  input;
  if _n_&amp;gt;1 then stop;
run;

%if &amp;amp;Flag &amp;gt; 1 %then %do;
proc import Datafile = "&amp;amp;CSVName" dbms = csv 
  out=&amp;amp;dsname replace
;
run;
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's look at how to integrate this into your previous program that is finding the file names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro importfile(location);
data files ;
  length filename $256 dsname $51;
  rc = filename('_dir_',symget('location'));
  if rc then put 'ERROR: Unable to find location.';
  else do;
    handle = dopen('_dir_');
    if handle &amp;gt; 0 then do;
      do i = 1 to dnum(handle);
        filename=catx('/',symget('location'),dread(handle,i));
        dsname = scan(filename,-1,'/\');
        dsname = substrn(dsname,1,min(32,length(dsname)-4));
        dsname = nliteral(dsname);
        if lowcase(scan(filename,-1,'.'))='csv' then do;
          call execute(catx(' ','%nrstr(%importcsv)(',filename,',',dsname,')'))
        ;
      end;
      rc=dclose(handle);
    end;
    else put 'ERROR: Unable to open location as a directory.';
    rc=filename('_dir_');
  end;
  keep filename dsname;
run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2020 16:25:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/706998#M217062</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-18T16:25:37Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through csv files and import</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/707000#M217063</link>
      <description>&lt;P&gt;Do you really want to run PROC IMPORT independently on each file?&amp;nbsp; If the files are supposed to contain the same fields then running PROC IMPORT on each one independently will lead to SAS datasets that are not compatible.&amp;nbsp; So if the files are supposed to have the same structure just read them all in one data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
   length fname filename $256 ;
   infile "&amp;amp;location/*.csv" filename=fname truncover dsd ;
*** Read and hold line so can test if start of a new file ;
   input @;
   if fname ne lag(fname) then input;
   else do;
     filename = fname ;
**** Put Code to read a line from the file here ***;
     input ..... ;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Dec 2020 16:30:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/707000#M217063</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-18T16:30:24Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through csv files and import</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/707011#M217067</link>
      <description>&lt;P&gt;The code is intended to read all data from all csv files. Add variables as needed (take them from the file documentation) to the INPUT statement (where I put the dots). The filenames from the FILENAME= option are kept only for later reference.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2020 17:08:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/707011#M217067</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-12-18T17:08:42Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through csv files and import</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/783719#M249984</link>
      <description>&lt;P&gt;Does this work for .stc files too?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 18:04:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/783719#M249984</guid>
      <dc:creator>Sudeep_Neupane</dc:creator>
      <dc:date>2021-12-02T18:04:45Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through csv files and import</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/783736#M249993</link>
      <description>&lt;P&gt;It works for all&amp;nbsp;&lt;EM&gt;text&lt;/EM&gt; files that can be read with data steps.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 18:52:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-csv-files-and-import/m-p/783736#M249993</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-12-02T18:52:10Z</dc:date>
    </item>
  </channel>
</rss>

