Hi,
I run a macro to check if a csv file exist before importing it.
When it run the first time, as the csv file is not there yet, it give me a error (instead of move on to Else condition).
That happen several times.
Do you guys know what is going on and how to fix?
Many thanks,
HHC
%macro import_csv(file_path);
%if %sysfunc(fileexist(&file_path.)) %then %do;
proc import datafile="&file_path" out = runaway_1 replace; run; %end;
%else %do;
%put no data yet; %end;
%mend;
%import_csv(E:\Run_Away.csv);
The code is checking for one file, &file_path, and then attempting to read a different file, &path.
If the file actually does exist and is not empty and you get that error when using DBMS=CSV in PROC IMPORT than it is usually because the file was created using only CR as the end of line character. So it looks like has only ONE line. If that is what is happening then make a FILEREF so you can use the TERMSTR=CR option.
filename csv "&filepath" termstr=cr ;
proc import datafile=csv dbms=csv out=runaway_1 replace;
run;
Thanks, Tom.
I fixed the typo.
The error shown when the file doesn't exist 😕
HHC
This macro will only cause you pain. NEVER use PROC IMPORT for text (csv) files, always write the DATA step yourself.
To make that simple check, no macro is needed, %IF can be used in "open code".
If SAS finds a file is there, then it is there, period. So you have to look at the file in question and see what's in there (and take the necessary information for writing the DATA step from the file documentation).
Can you show the full log you get, including the ERROR message, when the file does not exist?
I ran your code as is, with a file that does not exist, and the %PUT statement executed fine, no error message.
1 %macro import_csv(file_path); 2 %if %sysfunc(fileexist(&file_path.)) %then %do; 3 proc import datafile="&file_path" out = runaway_1 replace; run; %end; 4 %else %do; 5 %put no data yet; %end; 6 %mend; 7 %import_csv(Q:\Junk\NotThere) no data yet
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
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.