<?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: Proc Import for Batch CSVs - Naming the Imported Files in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-Import-for-Batch-CSVs-Naming-the-Imported-Files/m-p/736597#M229503</link>
    <description>&lt;P&gt;Why are using the DOPEN(), DREAD() functions in macro code instead of data step?&amp;nbsp; Your macro is already generating SAS steps so there is no need to complicate it by using macro code to handle the filename data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro drive(dir,ext); 
data files ;
  length filename $256 memname $32 ;
  rc=filename('mydir',symget('dir'));
  did=dopen('mydir');
  do i=1 to dnum(did);
    filename=dread(did,i);
    if symget('ext')=scan(filename,-1,'.') then do;
      memname = scan(filename,1,'.');
      output;
      call execute(cats(' '
         ,'proc import datafile='
         ,quote(catx('\',symget('dir'),filename))
         ,'dbms=csv'
         ,'out=',memname,'replace'
         ,';run;'
      ));
    end;
  end;
  rc=dclose(did);
  rc=filename('mydir');
run;
%mend drive ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What if the name of the CSV is not valid to be used as the name of a dataset?&lt;/P&gt;</description>
    <pubDate>Fri, 23 Apr 2021 15:00:26 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-04-23T15:00:26Z</dc:date>
    <item>
      <title>Proc Import for Batch CSVs - Naming the Imported Files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-Import-for-Batch-CSVs-Naming-the-Imported-Files/m-p/736595#M229501</link>
      <description>&lt;P&gt;Friends,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following code works beautifully for batch importing CSV files. I'm asking for a basic tweak.&amp;nbsp; I would like the imported sas file to have the same name as the actual csv being imported. I've been playing around with the "out=dsn&amp;amp;cnt" command line, but can't seem to find the right logic.&amp;nbsp; Suggestions?&lt;/P&gt;&lt;PRE&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;            
          run;          
       %end; 
      %end;  

    %end;
      %end;
  %else %put &amp;amp;dir cannot be opened.;
  %let rc=%sysfunc(dclose(&amp;amp;did));      
             
 %mend drive;
 
%drive(c:\temp,csv)&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Apr 2021 14:44:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-Import-for-Batch-CSVs-Naming-the-Imported-Files/m-p/736595#M229501</guid>
      <dc:creator>eer_seer</dc:creator>
      <dc:date>2021-04-23T14:44:49Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Import for Batch CSVs - Naming the Imported Files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-Import-for-Batch-CSVs-Naming-the-Imported-Files/m-p/736597#M229503</link>
      <description>&lt;P&gt;Why are using the DOPEN(), DREAD() functions in macro code instead of data step?&amp;nbsp; Your macro is already generating SAS steps so there is no need to complicate it by using macro code to handle the filename data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro drive(dir,ext); 
data files ;
  length filename $256 memname $32 ;
  rc=filename('mydir',symget('dir'));
  did=dopen('mydir');
  do i=1 to dnum(did);
    filename=dread(did,i);
    if symget('ext')=scan(filename,-1,'.') then do;
      memname = scan(filename,1,'.');
      output;
      call execute(cats(' '
         ,'proc import datafile='
         ,quote(catx('\',symget('dir'),filename))
         ,'dbms=csv'
         ,'out=',memname,'replace'
         ,';run;'
      ));
    end;
  end;
  rc=dclose(did);
  rc=filename('mydir');
run;
%mend drive ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What if the name of the CSV is not valid to be used as the name of a dataset?&lt;/P&gt;</description>
      <pubDate>Fri, 23 Apr 2021 15:00:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-Import-for-Batch-CSVs-Naming-the-Imported-Files/m-p/736597#M229503</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-04-23T15:00:26Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Import for Batch CSVs - Naming the Imported Files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-Import-for-Batch-CSVs-Naming-the-Imported-Files/m-p/736604#M229509</link>
      <description>&lt;P&gt;Thank you.&amp;nbsp; I took the code straight out of the sas documentation?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&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;</description>
      <pubDate>Fri, 23 Apr 2021 15:18:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-Import-for-Batch-CSVs-Naming-the-Imported-Files/m-p/736604#M229509</guid>
      <dc:creator>eer_seer</dc:creator>
      <dc:date>2021-04-23T15:18:37Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Import for Batch CSVs - Naming the Imported Files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-Import-for-Batch-CSVs-Naming-the-Imported-Files/m-p/736605#M229510</link>
      <description>&lt;P&gt;The page you linked is from the SAS Macro Language documentation, which explains the overuse of macro code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS frequently does not share very good code.&amp;nbsp; Just look at the gibberish data steps that PROC IMPORT will generate for all of those CSV files.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Apr 2021 15:42:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-Import-for-Batch-CSVs-Naming-the-Imported-Files/m-p/736605#M229510</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-04-23T15:42:23Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Import for Batch CSVs - Naming the Imported Files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-Import-for-Batch-CSVs-Naming-the-Imported-Files/m-p/736607#M229512</link>
      <description>&lt;P&gt;I appreciate you engaging me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not sure what I need to put into your suggested code to make it work.&amp;nbsp; Where do I put my directory I want SAS to read into your code?&lt;/P&gt;</description>
      <pubDate>Fri, 23 Apr 2021 15:31:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-Import-for-Batch-CSVs-Naming-the-Imported-Files/m-p/736607#M229512</guid>
      <dc:creator>eer_seer</dc:creator>
      <dc:date>2021-04-23T15:31:00Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Import for Batch CSVs - Naming the Imported Files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-Import-for-Batch-CSVs-Naming-the-Imported-Files/m-p/736609#M229514</link>
      <description>&lt;P&gt;Try replacing this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;          proc import datafile="&amp;amp;dir\%qsysfunc(dread(&amp;amp;did,&amp;amp;i))" out=dsn&amp;amp;cnt 
             dbms=csv replace;    &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;With:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;          proc import datafile="&amp;amp;dir\%qsysfunc(dread(&amp;amp;did,&amp;amp;i))" out=%qsysfunc(dread(&amp;amp;did,&amp;amp;i))
             dbms=csv replace;    &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;From your original code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Apr 2021 15:40:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-Import-for-Batch-CSVs-Naming-the-Imported-Files/m-p/736609#M229514</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-04-23T15:40:46Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Import for Batch CSVs - Naming the Imported Files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-Import-for-Batch-CSVs-Naming-the-Imported-Files/m-p/736610#M229515</link>
      <description>&lt;P&gt;The replacement macro takes the same inputs as the example macro you posted in your question. So call it the same way.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%drive(c:\temp,csv)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Apr 2021 15:37:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-Import-for-Batch-CSVs-Naming-the-Imported-Files/m-p/736610#M229515</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-04-23T15:37:58Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Import for Batch CSVs - Naming the Imported Files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-Import-for-Batch-CSVs-Naming-the-Imported-Files/m-p/736616#M229517</link>
      <description>&lt;LI-CODE lang="sas"&gt;NOTE: PROCEDURE IMPORT used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      
ERROR: Libref 'ef2009d_rv' exceeds 8 characters.
NOTE: The SAS System stopped processing this step because of errors.
ef2010d_rv.csv


NOTE: PROCEDURE IMPORT used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds&lt;/LI-CODE&gt;
&lt;P&gt;Those are the errors I get when trying your suggestion to replace the original proc import with below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; proc import datafile="&amp;amp;dir\%qsysfunc(dread(&amp;amp;did,&amp;amp;i))" out=%qsysfunc(dread(&amp;amp;did,&amp;amp;i))
             dbms=csv replace;   &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Apr 2021 15:46:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-Import-for-Batch-CSVs-Naming-the-Imported-Files/m-p/736616#M229517</guid>
      <dc:creator>eer_seer</dc:creator>
      <dc:date>2021-04-23T15:46:56Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Import for Batch CSVs - Naming the Imported Files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-Import-for-Batch-CSVs-Naming-the-Imported-Files/m-p/736618#M229519</link>
      <description>Show more of the log, specifically the line above that will show what the macro variable resolves to.&lt;BR /&gt;You may be running into the issue Tom mentioned, what if the CSV file doesn't have a valid SAS name. SAS names must be 32 characters or less, cannot start with a number, and can only contain alphanumeric characters and underscores. You can get around this in a few ways but it usually ends up causing issues later on. What are you planning to do with all of these files? If you need to combine them via append in the end, you can actually read them all in a single non-macro data step.</description>
      <pubDate>Fri, 23 Apr 2021 15:57:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-Import-for-Batch-CSVs-Naming-the-Imported-Files/m-p/736618#M229519</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-04-23T15:57:56Z</dc:date>
    </item>
  </channel>
</rss>

