Check if this helps for you. Change the import for the files you have (In my case xlsx files)
%let Path=<Your path here>;
/* Read all the files that are in a specific folder */
FILENAME _folder_ "%bquote(&path.)";
data filenames(keep=memname);
FORMAT memname $100.;
handle=dopen( '_folder_' );
if handle > 0 then do;
count=dnum(handle);
do i=1 to count;
memname=dread(handle,i);
output filenames;
end;
end;
rc=dclose(handle);
run;
filename _folder_ clear;
/* Count total files in the location and add all the file names into a macro */
PROC SQL noprint;
SELECT COUNT(*),memname INTO: Total_Files, : All_Files separated by "," FROM filenames;
QUIT;
%PUT "Total Files:" &Total_Files "All Files:" &All_Files;
/* Close files if they are opened before importing into SAS. If they are not closed then you may get an error */
/* If you get error then try re-running it aganin */
%MACRO CLOSE_FIELS();
%DO I=1 %TO &Total_Files ;
%LET File=%SCAN("&All_Files",&i,",");
%let filrf=myfile;
%let rc=%sysfunc(filename(filrf,&path./&FILE.));
%let fid=%sysfunc(fopen(&filrf));
%if &fid > 0 %then
%do;
%let rc=%sysfunc(fread(&fid));
%let rc=%sysfunc(fclose(&fid));
%end;
%else
%do;
%put %sysfunc(sysmsg());
%end;
%let rc=%sysfunc(filename(filrf));
%END;
%MEND;
%CLOSE_FIELS;
/* Macro to import all the files from path to SAS */
/* If you get an error then re-run this code again. If the excel file is opened then you may get an error */
%MACRO IMPORT_FILES();
%DO I=1 %TO &Total_Files ;
%LET File=%SCAN("&All_Files",&i,",");
proc import
datafile="&path./&File."
dbms=xlsx
out=GRIDWORK.TASK&i.
replace;
RUN;
%END;
%MEND IMPORT_FILES;
%IMPORT_FILES();
/* Append imported data into a single dataset */
%MACRO APPEND();
%DO I=2 %TO &Total_Files;
PROC APPEND BASE=GRIDWORK.Task1 DATA=GRIDWORK.Task&i. FORCE;
RUN;
%END;
%MEND APPEND;
%APPEND();
Hi @Uzi,
The log clearly says that "\\ayl-celerra-new\work\li\Moses\2017\Output\Check\" this path is not known to SAS. Looks like this is a network drive and not the path in Server where SAS is installed. Make sure your files are in a location where SAS can access them.
Check if this helps for you. Change the import for the files you have (In my case xlsx files)
%let Path=<Your path here>;
/* Read all the files that are in a specific folder */
FILENAME _folder_ "%bquote(&path.)";
data filenames(keep=memname);
FORMAT memname $100.;
handle=dopen( '_folder_' );
if handle > 0 then do;
count=dnum(handle);
do i=1 to count;
memname=dread(handle,i);
output filenames;
end;
end;
rc=dclose(handle);
run;
filename _folder_ clear;
/* Count total files in the location and add all the file names into a macro */
PROC SQL noprint;
SELECT COUNT(*),memname INTO: Total_Files, : All_Files separated by "," FROM filenames;
QUIT;
%PUT "Total Files:" &Total_Files "All Files:" &All_Files;
/* Close files if they are opened before importing into SAS. If they are not closed then you may get an error */
/* If you get error then try re-running it aganin */
%MACRO CLOSE_FIELS();
%DO I=1 %TO &Total_Files ;
%LET File=%SCAN("&All_Files",&i,",");
%let filrf=myfile;
%let rc=%sysfunc(filename(filrf,&path./&FILE.));
%let fid=%sysfunc(fopen(&filrf));
%if &fid > 0 %then
%do;
%let rc=%sysfunc(fread(&fid));
%let rc=%sysfunc(fclose(&fid));
%end;
%else
%do;
%put %sysfunc(sysmsg());
%end;
%let rc=%sysfunc(filename(filrf));
%END;
%MEND;
%CLOSE_FIELS;
/* Macro to import all the files from path to SAS */
/* If you get an error then re-run this code again. If the excel file is opened then you may get an error */
%MACRO IMPORT_FILES();
%DO I=1 %TO &Total_Files ;
%LET File=%SCAN("&All_Files",&i,",");
proc import
datafile="&path./&File."
dbms=xlsx
out=GRIDWORK.TASK&i.
replace;
RUN;
%END;
%MEND IMPORT_FILES;
%IMPORT_FILES();
/* Append imported data into a single dataset */
%MACRO APPEND();
%DO I=2 %TO &Total_Files;
PROC APPEND BASE=GRIDWORK.Task1 DATA=GRIDWORK.Task&i. FORCE;
RUN;
%END;
%MEND APPEND;
%APPEND();
@Uzi ,
I have "GRIDWORK" library in my code as my temporary work library. You may need to change that to "WORK". The log says "GRIDWORK" library is not assigned.
Make sure to use the {i} or Running man ICON to paste your log and/or code.
Is the problem that you searched for files in a directory and then tried to open the file without prefixing the directory name?
@Uzi wrote:
Hi Tom,
I'm not sure I understood the question ..
Suryakiran, i have another question:
what should be done to this code if you want to create a macro that can read data from five different folders in a different computers in a loop
Your error message is listing a filename that is not a fully qualified filename. It does not tell SAS what directory to look in.
filename=EV0_IF2016Q4_16f_BE16.csv
If you are on Windows then the filename should look something like C:\somedir1\anotherdir2\EV0_IF2016Q4_16f_BE16.csv. If your SAS session is running on Unix then it should look like /somedir1/anotherdir2/EV0_IF2016Q4_16f_BE16.csv. Also on Unix the filenames are case sensitive.
@Uzi wrote:
Hey Experts,
after a few attempts I realized I had a problem.My data is changing. For example, a policy number can appear once a number and once a number and a letter.
The proc import step checks for the first few lines(20) and then determines the format of the field. In my case, the program encountered only a number and therefore determined numerical while it could also be a charcter.
Is it possible to update SuryKirn's program so it will know determine the format of the fields correctly?
A reminder:
I need to select only a few files (a specific files) from the folder (not all the files in the folder) and apend them in to one file.
Thanks in advance for the help and forgiveness for my ignorance in sas.
For CSV files you can change the GUESSINGROWS option, but that won't solve the problem if the particular subset of data in that CSV file happens to only have ID codes that look like numbers. If you don't want SAS to guess at how to define your variables then do not use PROC IMPORT to read your files. Instead write you own data step to read the files and you will have complete control over how the variables are defined.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.