<?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 Datasets Using A Macro in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Importing-Datasets-Using-A-Macro/m-p/613555#M18443</link>
    <description>&lt;P&gt;Assuming csv has the same structure.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let target=c:\temp;/* specify target folder including xls files. */
filename cmd pipe "dir /b &amp;amp;target | findstr .csv";
data _null_;
  infile cmd;
  input;
  call symputx(cats('csvf',_n_),_infile_);
  call symputx('nobs',_n_);
run;

%Macro MimportCSV;
  %do i=1 %to &amp;amp;nobs;
    filename csvf "&amp;amp;target\&amp;amp;&amp;amp;csvf&amp;amp;i";
    data csv_&amp;amp;i.;
      infile csvf dsd;
      /* describe input statement... */
    run;
    filename csvf;
  %end;
%Mend MimportCSV;
%MimportCSV;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 23 Dec 2019 16:20:56 GMT</pubDate>
    <dc:creator>japelin</dc:creator>
    <dc:date>2019-12-23T16:20:56Z</dc:date>
    <item>
      <title>Importing Datasets Using A Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Importing-Datasets-Using-A-Macro/m-p/613545#M18438</link>
      <description>&lt;P&gt;I have a folder with multiple csv files that need to be imported in SAS. How can I do this using a macro? There are many files within this folder. There are also folders with only one or two csv files. Thanks for the help everyone.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Dec 2019 15:29:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Importing-Datasets-Using-A-Macro/m-p/613545#M18438</guid>
      <dc:creator>pw7632</dc:creator>
      <dc:date>2019-12-23T15:29:19Z</dc:date>
    </item>
    <item>
      <title>Re: Importing Datasets Using A Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Importing-Datasets-Using-A-Macro/m-p/613546#M18439</link>
      <description>&lt;P&gt;If all the CSV files have the EXACT same variable names and layout, no macro is needed. Import one using PROC IMPORT, modify the code in the log so that the the INFILE statement now uses an asterisk in place of the actual file name. This will create code that imports all the *.csv files in the folder.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;infile "c:\myfolder\*.csv" /* other options as needed */ ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the files have different variable names or different layouts, then you might need a macro.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Dec 2019 15:45:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Importing-Datasets-Using-A-Macro/m-p/613546#M18439</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-23T15:45:23Z</dc:date>
    </item>
    <item>
      <title>Re: Importing Datasets Using A Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Importing-Datasets-Using-A-Macro/m-p/613547#M18440</link>
      <description>&lt;P&gt;Thats the problem I need the files loaded separately. I need this macro to import them because then I need to find the contents of each separate file.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Dec 2019 15:45:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Importing-Datasets-Using-A-Macro/m-p/613547#M18440</guid>
      <dc:creator>pw7632</dc:creator>
      <dc:date>2019-12-23T15:45:32Z</dc:date>
    </item>
    <item>
      <title>Re: Importing Datasets Using A Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Importing-Datasets-Using-A-Macro/m-p/613549#M18441</link>
      <description>&lt;P&gt;It's still probably easier to do it this way than write a macro. All the .csv files wind up in a single SAS data set, the FILENAME= options stores the name of the original csv file in the SAS data set, and then you can use SAS to split the data apart if that's what you need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example using the PROC IMPORT code written to the log as a starting point.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydatasetname;
     length filename $ 256;
     infile "c:\myfolder\*.csv" /* other options from PROC IMPORT log */
        filename=filename;
     /* Other statements from PROC IMPORT log */
     originalfilename=filename;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Dec 2019 16:00:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Importing-Datasets-Using-A-Macro/m-p/613549#M18441</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-23T16:00:39Z</dc:date>
    </item>
    <item>
      <title>Re: Importing Datasets Using A Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Importing-Datasets-Using-A-Macro/m-p/613551#M18442</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you can do is first read the files that are in your folder and sub-folder if applicable and have the names of the files in a dataset. Later you can run a dataset driven program as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Reading the files in your folder: My examples is for Unix, but also should work for Windows after changing to correct path names (Forward slashes to backward)&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro Files_List(dsn, /* Output Dataset name. &amp;lt;libref&amp;gt;.&amp;lt;dataset name&amp;gt; e.g: Work.Files        */
			      dir, /* Directory name(Unix/Linux only). For windows need to change slashes in below code */
			      ext  /* File extension  for search without period. i.e: sas or xlsx or txt or csv  */  
				);   
   %if %sysfunc(exist(&amp;amp;dsn))=0 %then %do;

	/* Create a table */
		proc sql;
		create table &amp;amp;dsn  
				( File_Name char(1000) format=$1000. 
				);
		quit;

   %end;
 
  %local filrf rc did memcnt name i;                                                                                                    
                                                                                                                                        
  /* Assigns a fileref to the directory and opens the directory */                                                           
  %let rc=%sysfunc(filename(filrf,&amp;amp;dir));                                                                                               
  %let did=%sysfunc(dopen(&amp;amp;filrf));                                                                                                     
  /* Make sure directory can be open */                                                                                                 
  %if &amp;amp;did eq 0 %then %do;                                                                                                              
   %put Directory &amp;amp;dir cannot be open or does not exist;                                                                                
   %return;                                                                                                                             
  %end;                                                                                                                                 
                                                                                                                                        
   /* Loops through entire directory */                                                                                                 
   %do i = 1 %to %sysfunc(dnum(&amp;amp;did));                                                                                                  
                                                                                                                                        
     /* Retrieve name of each file */                                                                                                   
     %let name=%qsysfunc(dread(&amp;amp;did,&amp;amp;i));                                                                                               
                                                                                                                                        
     /* Checks to see if the extension matches the parameter value */                                                                   
     /* If condition is true print the full name to the log        */                                                                   
      %if %qupcase(%qscan(&amp;amp;name,-1,.)) = %upcase(&amp;amp;ext) %then %do; 
     /* Insert directory path name into table */
		proc sql;
		insert into &amp;amp;dsn
			set File_Name= "&amp;amp;dir/&amp;amp;name";
		quit;
      %end;                                                                                                                             
     /* If directory name call macro again */                                                                                           
      %else %if %qscan(&amp;amp;name,2,.) = %then %do;                                                                                          
        %Files_List(&amp;amp;dsn,&amp;amp;dir/%unquote(&amp;amp;name),&amp;amp;ext)                                                                                               
      %end;                                                                                                                             
                                                                                                                                        
   %end;   

                                                                                                                                        
  /* Closes the directory and clear the fileref */                                                                                      
  %let rc=%sysfunc(dclose(&amp;amp;did));                                                                                                       
  %let rc=%sysfunc(filename(filrf));                                                                                                    
                                                                                                                                        
%mend Files_List; 

/* Invoke the macro, example */
%Files_List(work.test,/user/KiranP,csv);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now you will have the files (ending CSV ) into a dataset. Using this dataset &amp;amp; Proc Import you can load your csv file to SAS Datasets.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set test;

call execute( cat( "proc import datafile='",trim(File_Name),"' out= ",scan(scan(File_Name,-1,'/'),1,'.') ," dbms=CSV;run;"));

run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: Few things you may have to take into consideration.&lt;/P&gt;
&lt;P&gt;1) Are your csv files all Unique, if no then the dataset will be overwritten. Consider changing the dataset names in Proc Import to something Unique even though the file names are not unique.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) Does proc import really works for you? It works on Guessing rows which you might have to consider, check for data truncation. example: if your columns first 20 rows are having length of 15 or below then proc import will consider a length of 15, but you might have records where length is &amp;gt;15 for later records, in this case your data is truncated to 15. You can use guessingrows=&amp;lt;number&amp;gt; as option for CSV files.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Dec 2019 16:08:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Importing-Datasets-Using-A-Macro/m-p/613551#M18442</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2019-12-23T16:08:50Z</dc:date>
    </item>
    <item>
      <title>Re: Importing Datasets Using A Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Importing-Datasets-Using-A-Macro/m-p/613555#M18443</link>
      <description>&lt;P&gt;Assuming csv has the same structure.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let target=c:\temp;/* specify target folder including xls files. */
filename cmd pipe "dir /b &amp;amp;target | findstr .csv";
data _null_;
  infile cmd;
  input;
  call symputx(cats('csvf',_n_),_infile_);
  call symputx('nobs',_n_);
run;

%Macro MimportCSV;
  %do i=1 %to &amp;amp;nobs;
    filename csvf "&amp;amp;target\&amp;amp;&amp;amp;csvf&amp;amp;i";
    data csv_&amp;amp;i.;
      infile csvf dsd;
      /* describe input statement... */
    run;
    filename csvf;
  %end;
%Mend MimportCSV;
%MimportCSV;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Dec 2019 16:20:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Importing-Datasets-Using-A-Macro/m-p/613555#M18443</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2019-12-23T16:20:56Z</dc:date>
    </item>
    <item>
      <title>Re: Importing Datasets Using A Macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Importing-Datasets-Using-A-Macro/m-p/613557#M18444</link>
      <description>&lt;P&gt;modidied.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%Macro MimportCSV(dir);
  filename cmd pipe "dir /b &amp;amp;dir | findstr .csv";
  data _null_;
    infile cmd;
    input;
    call symputx(cats('csvf',_n_),_infile_);
    call symputx('nobs',_n_);
  run;
  %do i=1 %to &amp;amp;nobs;
    proc import datafile="&amp;amp;dir\&amp;amp;&amp;amp;csvf&amp;amp;i"
                out=csv_&amp;amp;i
                dbms=CSV;
    run;
  %end;
%Mend MimportCSV;

%MimportCSV(%nrstr(c:\temp)); /* specify folder */&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Dec 2019 16:28:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Importing-Datasets-Using-A-Macro/m-p/613557#M18444</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2019-12-23T16:28:19Z</dc:date>
    </item>
  </channel>
</rss>

