<?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 Files at Once and Keeping File name as Dataset Name in SAS in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Importing-Multiple-Files-at-Once-and-Keeping-File-name-as/m-p/728017#M28252</link>
    <description>&lt;P&gt;Using macro code to call the data step functions is just going to make the code harder.&amp;nbsp; Your macro is going to generate SAS statements anyway so just use a data step to get the list of file names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let path=C:\path
%let ext=csv;

data files ;
  length memname $32 filename $256 ;
  rc = filename('dir',"&amp;amp;dir");
  did = dopen('dir');
  do index=1 to dnum(did);
    filename=catx('\',"&amp;amp;dir",dread(did,index));
    if upcase(scan(filename,-1,'.'))=%upcase("&amp;amp;ext") then do;
      memname=scan(filename,-2,'./');
      output;
    end;
  end;
  rc = dclose('dir');
  rc = filename('dir');
  drop rc did index;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Once you have the list of filenames then it is easy to use it to generate SAS code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set files;
  call execute(catx(' '
    ,'proc import datafile=',quote(trim(filename))
    ,'dbms=csv'
    ,'out=',memname,'replace'
    ,';run;'
  ));
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But&amp;nbsp;I am not sure using PROC IMPORT to read a series of text files is the right thing to do.&amp;nbsp; PROC IMPORT has to GUESS how to define the fields so each dataset could end up with slightly different structure.&amp;nbsp; It is trivial to read a text file with your own data step.&amp;nbsp; I am also not sure what value you get from putting data into different dataset, just leave the data for all of the stocks in the same dataset.&amp;nbsp; Here is the basic code. Just fill in the details of how to define the variables and read the line from the CSV file into the DO loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set files ;
  fname=filename;
  infile csv dsd filevar=fname firstobs=2 truncover end=eof;
  do while (not eof);
     input .... ;
     output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could even skip the whole step of searching for the filenames and just let the INFILE statement do that for you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  length fname $256 ;
  infile "&amp;amp;dir\*.&amp;amp;ext" dsd filename=fname truncover ;
  input @;
  filename=fname;
  if fname ne lag(fname) then input;
  length stock $10 ;
  stock = scan(filename,-2,'\.');
  input .... ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 21 Mar 2021 19:07:56 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-03-21T19:07:56Z</dc:date>
    <item>
      <title>Importing Multiple Files at Once and Keeping File name as Dataset Name in SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Importing-Multiple-Files-at-Once-and-Keeping-File-name-as/m-p/728012#M28249</link>
      <description>&lt;P&gt;Hi All&lt;/P&gt;&lt;P&gt;I have about 1000 csv files I need to import into SAS from a folder. I found this macro on SAS communities to batch import them, however, it changes the name of the files to dsn1, dsn2, dsn3 etc. and I want to retain the file name as the dataset name in SAS. All the files are stock ticker names (i.e. AAPL.csv MSFT.csv etc.) and I want the imported dataset to be AAPL, MSFT etc. Here is the code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro drive(dir,ext);&lt;BR /&gt;%local cnt filrf rc did memcnt name;&lt;BR /&gt;%let cnt=0;&lt;/P&gt;&lt;P&gt;%let filrf=mydir;&lt;BR /&gt;%let rc=%sysfunc(filename(filrf,&amp;amp;dir));&lt;BR /&gt;%let did=%sysfunc(dopen(&amp;amp;filrf));&lt;BR /&gt;%if &amp;amp;did ne 0 %then %do;&lt;BR /&gt;%let memcnt=%sysfunc(dnum(&amp;amp;did));&lt;/P&gt;&lt;P&gt;%do i=1 %to &amp;amp;memcnt;&lt;BR /&gt;&lt;BR /&gt;%let name=%qscan(%qsysfunc(dread(&amp;amp;did,&amp;amp;i)),-1,.);&lt;BR /&gt;&lt;BR /&gt;%if %qupcase(%qsysfunc(dread(&amp;amp;did,&amp;amp;i))) ne %qupcase(&amp;amp;name) %then %do;&lt;BR /&gt;%if %superq(ext) = %superq(name) %then %do;&lt;BR /&gt;%let cnt=%eval(&amp;amp;cnt+1);&lt;BR /&gt;%put %qsysfunc(dread(&amp;amp;did,&amp;amp;i));&lt;BR /&gt;proc import datafile="&amp;amp;dir\%qsysfunc(dread(&amp;amp;did,&amp;amp;i))" out=Dsn&amp;amp;cnt&lt;BR /&gt;dbms=csv replace;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;%end;&lt;BR /&gt;%end;&lt;BR /&gt;%else %put &amp;amp;dir cannot be opened.;&lt;BR /&gt;%let rc=%sysfunc(dclose(&amp;amp;did));&lt;BR /&gt;&lt;BR /&gt;%mend drive;&lt;BR /&gt;&lt;BR /&gt;%drive(C:\path,csv);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be appreciated!&lt;/P&gt;</description>
      <pubDate>Sun, 21 Mar 2021 17:45:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Importing-Multiple-Files-at-Once-and-Keeping-File-name-as/m-p/728012#M28249</guid>
      <dc:creator>MellyJ13</dc:creator>
      <dc:date>2021-03-21T17:45:10Z</dc:date>
    </item>
    <item>
      <title>Re: Importing Multiple Files at Once and Keeping File name as Dataset Name in SAS</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Importing-Multiple-Files-at-Once-and-Keeping-File-name-as/m-p/728017#M28252</link>
      <description>&lt;P&gt;Using macro code to call the data step functions is just going to make the code harder.&amp;nbsp; Your macro is going to generate SAS statements anyway so just use a data step to get the list of file names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let path=C:\path
%let ext=csv;

data files ;
  length memname $32 filename $256 ;
  rc = filename('dir',"&amp;amp;dir");
  did = dopen('dir');
  do index=1 to dnum(did);
    filename=catx('\',"&amp;amp;dir",dread(did,index));
    if upcase(scan(filename,-1,'.'))=%upcase("&amp;amp;ext") then do;
      memname=scan(filename,-2,'./');
      output;
    end;
  end;
  rc = dclose('dir');
  rc = filename('dir');
  drop rc did index;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Once you have the list of filenames then it is easy to use it to generate SAS code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set files;
  call execute(catx(' '
    ,'proc import datafile=',quote(trim(filename))
    ,'dbms=csv'
    ,'out=',memname,'replace'
    ,';run;'
  ));
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But&amp;nbsp;I am not sure using PROC IMPORT to read a series of text files is the right thing to do.&amp;nbsp; PROC IMPORT has to GUESS how to define the fields so each dataset could end up with slightly different structure.&amp;nbsp; It is trivial to read a text file with your own data step.&amp;nbsp; I am also not sure what value you get from putting data into different dataset, just leave the data for all of the stocks in the same dataset.&amp;nbsp; Here is the basic code. Just fill in the details of how to define the variables and read the line from the CSV file into the DO loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set files ;
  fname=filename;
  infile csv dsd filevar=fname firstobs=2 truncover end=eof;
  do while (not eof);
     input .... ;
     output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could even skip the whole step of searching for the filenames and just let the INFILE statement do that for you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  length fname $256 ;
  infile "&amp;amp;dir\*.&amp;amp;ext" dsd filename=fname truncover ;
  input @;
  filename=fname;
  if fname ne lag(fname) then input;
  length stock $10 ;
  stock = scan(filename,-2,'\.');
  input .... ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 21 Mar 2021 19:07:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Importing-Multiple-Files-at-Once-and-Keeping-File-name-as/m-p/728017#M28252</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-03-21T19:07:56Z</dc:date>
    </item>
  </channel>
</rss>

