<?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: Macro for importing several csv files in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-importing-several-csv-files/m-p/717374#M221834</link>
    <description>Thanks Kurt for the comment, I just changed the macro code with the same variable...&lt;BR /&gt;&lt;BR /&gt;And you're right, there is another part of the code which will append all the CSV files but it has not been written yet so for now it will take only the last file.&lt;BR /&gt;&lt;BR /&gt;The second code it's what's not working with now and what's giving me trouble at this point.&lt;BR /&gt;&lt;BR /&gt;Thanks again&lt;BR /&gt;&lt;BR /&gt;Fer</description>
    <pubDate>Sun, 07 Feb 2021 10:09:02 GMT</pubDate>
    <dc:creator>japfvg</dc:creator>
    <dc:date>2021-02-07T10:09:02Z</dc:date>
    <item>
      <title>Macro for importing several csv files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-importing-several-csv-files/m-p/717369#M221830</link>
      <description>Hi all, hope everyone is doing great!!&lt;BR /&gt;Again asking for your precious help&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;I've this macro for importing one single file at once that works well:&lt;BR /&gt;%macro Carga(File);&lt;BR /&gt;data work.q1;&lt;BR /&gt;infile &amp;amp;amp;File. dlm='|' firstobs=2 missover;&lt;BR /&gt;input field1 : $char5.&lt;BR /&gt;field2 : $char5.;&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;BR /&gt;I have also a dataset containing all the csv files in a folder (the dataset is called "work.files" and the field with the csv file names is called "filename") which I need to load with the previous macro, I'm trying with this code but it's not working:&lt;BR /&gt;data _null_;&lt;BR /&gt;set work.files;&lt;BR /&gt;call execute(cats('%nrstr(%Carga(',filename,'))'));&lt;BR /&gt;run;&lt;BR /&gt;This last code was recomended by some of you guys for another problem (the parameters were date type instead of filenames) and worked very well so I was trying to create a similar code for this new challenge.&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;Any help would be greatly appreciated.&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;Thanks in advance.&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;Regards to all!!&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;Fer&lt;BR /&gt;&amp;amp;nbsp;</description>
      <pubDate>Sun, 07 Feb 2021 10:04:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-importing-several-csv-files/m-p/717369#M221830</guid>
      <dc:creator>japfvg</dc:creator>
      <dc:date>2021-02-07T10:04:40Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for importing several csv files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-importing-several-csv-files/m-p/717371#M221832</link>
      <description>&lt;P&gt;Your macro has a parameter called File which you never use in the macro.&lt;/P&gt;
&lt;P&gt;Instead you use a macro variable called Archivo, which is not set in your code (as posted).&lt;/P&gt;
&lt;P&gt;Your macro also uses a fixed name for the dataset, so after using the macro N times, only the Nth import will be stored in dataset q1. You need to devise a way of either feeding the dataset name to the macro via a second parameter, or to create a (valid) dataset name from the infile name within the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So let's bo back to square one: post the data step code that successfully imports a single file (without&amp;nbsp;&lt;STRONG&gt;any&lt;/STRONG&gt; macro code), and sufficient examples for the contents of your dataset files.&lt;/P&gt;</description>
      <pubDate>Sun, 07 Feb 2021 09:46:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-importing-several-csv-files/m-p/717371#M221832</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-07T09:46:50Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for importing several csv files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-importing-several-csv-files/m-p/717374#M221834</link>
      <description>Thanks Kurt for the comment, I just changed the macro code with the same variable...&lt;BR /&gt;&lt;BR /&gt;And you're right, there is another part of the code which will append all the CSV files but it has not been written yet so for now it will take only the last file.&lt;BR /&gt;&lt;BR /&gt;The second code it's what's not working with now and what's giving me trouble at this point.&lt;BR /&gt;&lt;BR /&gt;Thanks again&lt;BR /&gt;&lt;BR /&gt;Fer</description>
      <pubDate>Sun, 07 Feb 2021 10:09:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-importing-several-csv-files/m-p/717374#M221834</guid>
      <dc:creator>japfvg</dc:creator>
      <dc:date>2021-02-07T10:09:02Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for importing several csv files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-importing-several-csv-files/m-p/717378#M221836</link>
      <description>&lt;P&gt;If your goal is the import of&amp;nbsp;&lt;EM&gt;all&lt;/EM&gt; csv files into a&amp;nbsp;&lt;EM&gt;single&lt;/EM&gt; dataset, then no macro is needed. You can use your files dataset to supply filenames to the INFILE statement with the FILEVAR= option.&lt;/P&gt;</description>
      <pubDate>Sun, 07 Feb 2021 11:26:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-importing-several-csv-files/m-p/717378#M221836</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-07T11:26:41Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for importing several csv files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-importing-several-csv-files/m-p/717401#M221844</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/310332"&gt;@japfvg&lt;/a&gt;&amp;nbsp;Below code may help you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET MAIN_PATH=KEEP PATH HERE;

DATA have;
 RC=FILENAME("FILEREF","&amp;amp;MAIN_PATH.");
 MYDIR=DOPEN("FILEREF");
 IF MYDIR &amp;gt; 0 THEN 
    NUM=DNUM(MYDIR);
	 DO I=1 TO NUM;
			IF I &amp;gt; 0 THEN DO
			   FILE_NAME=DREAD(MYDIR,i);
			   OUTPUT;
	END;
	END;
	rc=dclose(mydir);
RUN;
DATA have1;
SET have;
extn=STRIP(SCAN(FILE_NAME,-1,'.'));
if extn eq 'csv';
RUN;
PROC SQL ;
SELECT FILE_NAME INTO :SUBFILE1 -  FROM HAVE1;
%LET N=&amp;amp;SQLOBS;
QUIT;
%LET DATA=DATASETNAME;

%MACRO CSV_ALL;
%DO I=1 %TO &amp;amp;N.;
PROC IMPORT DATAFILE="&amp;amp;MAIN_PATH\&amp;amp;&amp;amp;SUBFILE&amp;amp;I"
DBMS=CSV REPLACE OUT=&amp;amp;DATA&amp;amp;I;
RUN;
%END;
%MEND;

%CSV_ALL;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 07 Feb 2021 14:38:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-importing-several-csv-files/m-p/717401#M221844</guid>
      <dc:creator>singhsahab</dc:creator>
      <dc:date>2021-02-07T14:38:40Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for importing several csv files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-importing-several-csv-files/m-p/717403#M221846</link>
      <description>&lt;P&gt;No need for macro code.&amp;nbsp; Just use a single data step to read all of the text files at once.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
  set work.files;
  fname = filename ;
  infile text dsd  dlm='|' truncover firstobs=2 filevar=fname end=eof;
  do while(not eof);
    input field1 :$5. field2 :$5. field3 ;
    output;
  end;
run; 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 07 Feb 2021 15:10:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-importing-several-csv-files/m-p/717403#M221846</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-07T15:10:37Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for importing several csv files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-importing-several-csv-files/m-p/717784#M222029</link>
      <description>&lt;P&gt;Thanks to all for your quick responses!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tested the filevar option using this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.q1;
set work.files1;
fname = filename;
infile text filevar=fname end=eof dlm=';' firstobs=2 dsd missover;

do while(not eof);
	input
		eoddate : ddmmyy11.	
		numberx : $char10.
		stgeneral : $char4.
end;

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Dataset work.files1 contains two records of two csv files which are needed to be imported.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;After running the whole code, for some reason, the input with the filevar option is reading only the first record of every csv file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any idea why is happening this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again for all your support.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Fer&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 05:36:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-importing-several-csv-files/m-p/717784#M222029</guid>
      <dc:creator>japfvg</dc:creator>
      <dc:date>2021-02-09T05:36:02Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for importing several csv files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-importing-several-csv-files/m-p/717803#M222043</link>
      <description>&lt;P&gt;Look closely at codes posted. Your code misses the explicit OUTPUT statement in the DO loop, so you only have one implicit output at the end of the data step iteration, outputting the last record read from a file.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 08:40:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-importing-several-csv-files/m-p/717803#M222043</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-02-09T08:40:22Z</dc:date>
    </item>
  </channel>
</rss>

