If your goal is the import of all csv files into a single dataset, then no macro is needed. You can use your files dataset to supply filenames to the INFILE statement with the FILEVAR= option.
Your macro has a parameter called File which you never use in the macro.
Instead you use a macro variable called Archivo, which is not set in your code (as posted).
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.
So let's bo back to square one: post the data step code that successfully imports a single file (without any macro code), and sufficient examples for the contents of your dataset files.
If your goal is the import of all csv files into a single dataset, then no macro is needed. You can use your files dataset to supply filenames to the INFILE statement with the FILEVAR= option.
@japfvg Below code may help you
%LET MAIN_PATH=KEEP PATH HERE;
DATA have;
RC=FILENAME("FILEREF","&MAIN_PATH.");
MYDIR=DOPEN("FILEREF");
IF MYDIR > 0 THEN
NUM=DNUM(MYDIR);
DO I=1 TO NUM;
IF I > 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=&SQLOBS;
QUIT;
%LET DATA=DATASETNAME;
%MACRO CSV_ALL;
%DO I=1 %TO &N.;
PROC IMPORT DATAFILE="&MAIN_PATH\&&SUBFILE&I"
DBMS=CSV REPLACE OUT=&DATA&I;
RUN;
%END;
%MEND;
%CSV_ALL;
No need for macro code. Just use a single data step to read all of the text files at once.
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;
Thanks to all for your quick responses!!
I tested the filevar option using this:
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;
Dataset work.files1 contains two records of two csv files which are needed to be imported.
After running the whole code, for some reason, the input with the filevar option is reading only the first record of every csv file.
Any idea why is happening this?
Thanks again for all your support.
Fer
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.