<?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 Importing multiple csv files in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files/m-p/854378#M337650</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is an example macro from sas documentation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro drive(dir,ext); 
   %local cnt filrf rc did memcnt name;

   %let cnt=0;
   %let filrf=mydir;

   %let rc=%sysfunc(filename(filrf,&amp;amp;dir));
   %let did=%sysfunc(dopen(&amp;amp;filrf));

   %if &amp;amp;did ne 0 %then %do;
      %let memcnt=%sysfunc(dnum(&amp;amp;did));
      %do i=1 %to &amp;amp;memcnt;
         %let name=%qscan(%qsysfunc(dread(&amp;amp;did,&amp;amp;i)),-1,.);
         %if %qupcase(%qsysfunc(dread(&amp;amp;did,&amp;amp;i))) ne %qupcase(&amp;amp;name) %then %do;
            %if %superq(ext) = %superq(name) %then %do;
               %let cnt=%eval(&amp;amp;cnt+1);
               %put %qsysfunc(dread(&amp;amp;did,&amp;amp;i));
               proc import datafile="&amp;amp;dir\%qsysfunc(dread(&amp;amp;did,&amp;amp;i))" out=dsn&amp;amp;cnt
                  dbms=csv replace;
                  guessingrows=max;
               run;
            %end;
         %end;
       %end;
    %end;
  %else %put &amp;amp;dir cannot be opened.;

  %let rc=%sysfunc(dclose(&amp;amp;did));
%mend drive;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n0ctmldxf23ixtn1kqsoh5bsgmg8.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n0ctmldxf23ixtn1kqsoh5bsgmg8.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do I modify it, so that the imported files have the same name as the CSV file? Is this possible?&lt;/P&gt;</description>
    <pubDate>Wed, 18 Jan 2023 17:03:40 GMT</pubDate>
    <dc:creator>bharath86</dc:creator>
    <dc:date>2023-01-18T17:03:40Z</dc:date>
    <item>
      <title>Importing multiple csv files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files/m-p/854378#M337650</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is an example macro from sas documentation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro drive(dir,ext); 
   %local cnt filrf rc did memcnt name;

   %let cnt=0;
   %let filrf=mydir;

   %let rc=%sysfunc(filename(filrf,&amp;amp;dir));
   %let did=%sysfunc(dopen(&amp;amp;filrf));

   %if &amp;amp;did ne 0 %then %do;
      %let memcnt=%sysfunc(dnum(&amp;amp;did));
      %do i=1 %to &amp;amp;memcnt;
         %let name=%qscan(%qsysfunc(dread(&amp;amp;did,&amp;amp;i)),-1,.);
         %if %qupcase(%qsysfunc(dread(&amp;amp;did,&amp;amp;i))) ne %qupcase(&amp;amp;name) %then %do;
            %if %superq(ext) = %superq(name) %then %do;
               %let cnt=%eval(&amp;amp;cnt+1);
               %put %qsysfunc(dread(&amp;amp;did,&amp;amp;i));
               proc import datafile="&amp;amp;dir\%qsysfunc(dread(&amp;amp;did,&amp;amp;i))" out=dsn&amp;amp;cnt
                  dbms=csv replace;
                  guessingrows=max;
               run;
            %end;
         %end;
       %end;
    %end;
  %else %put &amp;amp;dir cannot be opened.;

  %let rc=%sysfunc(dclose(&amp;amp;did));
%mend drive;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n0ctmldxf23ixtn1kqsoh5bsgmg8.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n0ctmldxf23ixtn1kqsoh5bsgmg8.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do I modify it, so that the imported files have the same name as the CSV file? Is this possible?&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jan 2023 17:03:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files/m-p/854378#M337650</guid>
      <dc:creator>bharath86</dc:creator>
      <dc:date>2023-01-18T17:03:40Z</dc:date>
    </item>
    <item>
      <title>Re: Importing multiple csv files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files/m-p/854385#M337653</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;How do I modify it, so that the imported files have the same name as the CSV file? Is this possible?&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Only if the names of the CSV files are valid SAS dataset names.&amp;nbsp; For example if the CSV file is named "class.csv" then you could pull out the "class" part and use it as the dataset name.&amp;nbsp; But if the CSV file was named "gobbledy-gook.csv" then you could not use "gobbledy-gook" as the dataset names because dataset names cannot have a hyphen.&amp;nbsp; Or if the names is longer than 32 bytes you cannot use it as a dataset name.&amp;nbsp; You could use NVALID() function to test.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note it is probably going to be a lot easier to just use normal SAS code to get the list of filenames instead of using the macro code you posted.&amp;nbsp; It is waaaaaaaay easier to debug data steps than macro code.&amp;nbsp; You can use CALL EXECUTE() to generate the PROC IMPORT code if you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro drive(dir,ext,dataset);
data &amp;amp;dataset ;
  length dataset $32 filename $256 ;
  rc = filename('mydir',symget('dir'));
  did = dopen('mydir');
  do fnum=1 to dnum(did);
    filename=dread(did,fnum);
    if lowcase("&amp;amp;ext")=lowcase(scan(filename,-1,'.') then do;
      dataset=scan(filename,-2,'./\');
      if not nvalid(dataset) then dataset=cats('csv',fnum);
      filename=catx('/',symget('dir'),filename);
      call execute(catx(' ','proc import dbms=csv'
                ,'datafile=',quote(trim(filename))
                ,'out=',dataset,'replace'
                ,';run;'
      ));
      output;
    end;
  end;
  did = dclose(did);
  rc = filename('mydir');
  keep dataset filename
run;
%mend drive;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jan 2023 17:47:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files/m-p/854385#M337653</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-01-18T17:47:05Z</dc:date>
    </item>
    <item>
      <title>Re: Importing multiple csv files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files/m-p/854540#M337722</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;               proc import datafile="&amp;amp;dir\%qsysfunc(dread(&amp;amp;did,&amp;amp;i))" &lt;FONT color="#FF0000"&gt;out=%qscan(%qsysfunc(dread(&amp;amp;did,&amp;amp;i)),1,.)&lt;/FONT&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Jan 2023 11:46:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-multiple-csv-files/m-p/854540#M337722</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-01-19T11:46:13Z</dc:date>
    </item>
  </channel>
</rss>

