<?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 multiple csv files from a single directory with a loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files-from-a-single-directory-with-a-loop/m-p/872636#M344756</link>
    <description>&lt;P&gt;You cannot call a macro if you haven't defined it to SAS first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First submit the code that defines %FOR before submitting code that calls it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But there is no need to use a macro to loop over a list of values.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That is what a data step does already.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   length filename $256;
   do filename='filename_ak','filename_al','filename_ct';
      call execute(catx(' '
      ,'PROC IMPORT DATAFILE=',quote(cats('\\c:\datafiles\',filename,'.csv'))
      ,'OUT=',cats('WORK.',filename)
      ,'DBMS=CSV REPLACE;'
      ,'RUN;'
     ));
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;WARNING:&lt;/STRONG&gt; Using PROC IMPORT to read a series of CSV files that should have the exact same variables is a recipe for disaster.&amp;nbsp; PROC IMPORT will make DIFFERENT GUESSES about variable names, types, lengths and formats based on each file independently.&amp;nbsp; That will lead to datasets that are incompatible.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead write your own data step to read the CSV file(s) and you will have full control over how the variables are defined.&lt;/P&gt;</description>
    <pubDate>Thu, 27 Apr 2023 18:47:53 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-04-27T18:47:53Z</dc:date>
    <item>
      <title>Importing multiple csv files from a single directory with a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files-from-a-single-directory-with-a-loop/m-p/872633#M344753</link>
      <description>&lt;P&gt;I have a directory with a large number of csv files. I want to import some of them without writing a separate proc import for each, because that would be silly.&lt;/P&gt;&lt;P&gt;For arguments sake, let's say the files are named filename_ak.csv, filename_al.csv, filename_ct.csv.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A resource I found elsewhere suggested that the following macro would work;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%for(filename, values=filename_ak filename_al filename_ct, do=%nrstr(&lt;BR /&gt;PROC IMPORT&lt;BR /&gt;DATAFILE= "\\c:\datafiles\&amp;amp;filename.csv"&lt;BR /&gt;OUT=WORK.&amp;amp;filename&lt;BR /&gt;DBMS=CSV REPLACE;&lt;BR /&gt;RUN;&lt;BR /&gt;))&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I get error messages:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;WARNING: Apparent invocation of macro FOR not resolved.&lt;BR /&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am running SAS 9.4 on X64_10PRO&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2023 18:38:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files-from-a-single-directory-with-a-loop/m-p/872633#M344753</guid>
      <dc:creator>Thomasanderson</dc:creator>
      <dc:date>2023-04-27T18:38:26Z</dc:date>
    </item>
    <item>
      <title>Re: Importing multiple csv files from a single directory with a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files-from-a-single-directory-with-a-loop/m-p/872635#M344755</link>
      <description>&lt;PRE&gt;WARNING: Apparent invocation of macro FOR not resolved.&lt;/PRE&gt;
&lt;P&gt;A macro has to be defined in the program (or included via &lt;FONT face="courier new,courier"&gt;%include&lt;/FONT&gt;, or included via autocall libraries, or some other way). You haven't done that. SAS doesn't know what macro&amp;nbsp;&lt;FONT face="courier new,courier"&gt;%for&lt;/FONT&gt; is.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2023 18:43:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files-from-a-single-directory-with-a-loop/m-p/872635#M344755</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-04-27T18:43:15Z</dc:date>
    </item>
    <item>
      <title>Re: Importing multiple csv files from a single directory with a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files-from-a-single-directory-with-a-loop/m-p/872636#M344756</link>
      <description>&lt;P&gt;You cannot call a macro if you haven't defined it to SAS first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First submit the code that defines %FOR before submitting code that calls it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But there is no need to use a macro to loop over a list of values.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That is what a data step does already.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   length filename $256;
   do filename='filename_ak','filename_al','filename_ct';
      call execute(catx(' '
      ,'PROC IMPORT DATAFILE=',quote(cats('\\c:\datafiles\',filename,'.csv'))
      ,'OUT=',cats('WORK.',filename)
      ,'DBMS=CSV REPLACE;'
      ,'RUN;'
     ));
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;WARNING:&lt;/STRONG&gt; Using PROC IMPORT to read a series of CSV files that should have the exact same variables is a recipe for disaster.&amp;nbsp; PROC IMPORT will make DIFFERENT GUESSES about variable names, types, lengths and formats based on each file independently.&amp;nbsp; That will lead to datasets that are incompatible.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead write your own data step to read the CSV file(s) and you will have full control over how the variables are defined.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2023 18:47:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files-from-a-single-directory-with-a-loop/m-p/872636#M344756</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-04-27T18:47:53Z</dc:date>
    </item>
    <item>
      <title>Re: Importing multiple csv files from a single directory with a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files-from-a-single-directory-with-a-loop/m-p/872638#M344758</link>
      <description>&lt;P&gt;If these files have the same structure, and the data should end up in one dataset, do not use a macro and do not use PROC IMPORT.&lt;/P&gt;
&lt;P&gt;Write a data step which either uses a wildcard in the INFILE statement or reads filenames from a dataset and uses the FILEVAR= option.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2023 18:57:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files-from-a-single-directory-with-a-loop/m-p/872638#M344758</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-04-27T18:57:00Z</dc:date>
    </item>
    <item>
      <title>Re: Importing multiple csv files from a single directory with a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files-from-a-single-directory-with-a-loop/m-p/872639#M344759</link>
      <description>If they all have the same layout and you have a data step import code you can read them all at once using the * in the filename as a wildcard character. &lt;BR /&gt;</description>
      <pubDate>Thu, 27 Apr 2023 19:00:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files-from-a-single-directory-with-a-loop/m-p/872639#M344759</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-04-27T19:00:31Z</dc:date>
    </item>
    <item>
      <title>Re: Importing multiple csv files from a single directory with a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files-from-a-single-directory-with-a-loop/m-p/872867#M344860</link>
      <description>Thank you. I guess I have a lot to learn about macros.</description>
      <pubDate>Fri, 28 Apr 2023 16:29:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files-from-a-single-directory-with-a-loop/m-p/872867#M344860</guid>
      <dc:creator>Thomasanderson</dc:creator>
      <dc:date>2023-04-28T16:29:42Z</dc:date>
    </item>
    <item>
      <title>Re: Importing multiple csv files from a single directory with a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files-from-a-single-directory-with-a-loop/m-p/872871#M344862</link>
      <description>&lt;P&gt;UCLA introductory tutorial on macro variables and macros&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Tutorial on converting a working program to a macro&lt;BR /&gt;This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Examples of common macro usage&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2023 16:41:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files-from-a-single-directory-with-a-loop/m-p/872871#M344862</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-04-28T16:41:21Z</dc:date>
    </item>
    <item>
      <title>Re: Importing multiple csv files from a single directory with a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files-from-a-single-directory-with-a-loop/m-p/872929#M344899</link>
      <description>&lt;P&gt;Thanks again for this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can I use something like this to go through the datasets I have created and do some simple operations like "new_var = var1 + var 2;" and "keep new_var var3 var4;" ?&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2023 21:06:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files-from-a-single-directory-with-a-loop/m-p/872929#M344899</guid>
      <dc:creator>Thomasanderson</dc:creator>
      <dc:date>2023-04-28T21:06:54Z</dc:date>
    </item>
  </channel>
</rss>

