BookmarkSubscribeRSS Feed
hhchenfx
Barite | Level 11

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);
4 REPLIES 4
Tom
Super User Tom
Super User

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;
hhchenfx
Barite | Level 11

Thanks, Tom.

I fixed the typo.

The error shown when the file doesn't exist 😕

HHC

Kurt_Bremser
Super User

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).

Quentin
Super User

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
BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 417 views
  • 3 likes
  • 4 in conversation