I am importing multiple files in a database and I received a file with only headers and zero records. How can I bypass the file if it is empty?
Please explain... If the file is empty, it should contribute 0 records to your database when it is imported.
You could test the file before processing:
data _null_;
call symputx ('file_contains_data',_N_-1);
infile "%sysfunc(pathname(WORK))\t.txt" obs=2 firstobs=2;
input;
run;
%put &=file_contains_data;
Please show how you are currently importing the non-empty files.
I have created three files:
test1.csv:
vara,varb 1,2 3,4
test2.csv:
vara,varb
test3.csv:
vara,varb 5,6 7,8
Importing all files of similar structure in one data step:
data want1;
infile '$HOME/sascommunity/test*.csv' dlm=',' dsd truncover;
input @;
if not index(_infile_,'vara');
input vara varb;
run;
proc print data=want1 noobs;
run;
Result:
vara varb 1 2 3 4 5 6 7 8
The empty file was successfully "skipped".
Importing a single file and determining if empty on the fly:
data want2;
infile '$HOME/sascommunity/test2.csv' dlm=',' dsd truncover firstobs=2;
retain count 0;
call symputx('count',count);
input vara varb;
count + 1;
drop count;
run;
%if &count %then %do;
/* further processing of dataset */
%end;
%if not &count %then %do;
%put empty!;
%end;
Log:
27 data want2; 28 infile '$HOME/sascommunity/test2.csv' dlm=',' dsd truncover firstobs=2; 29 retain count 0; 30 call symputx('count',count); 31 input vara varb; 32 count + 1; 33 drop count; 34 run; NOTE: The infile '$HOME/sascommunity/test2.csv' is: Dateiname=/wadaten/home/e9782/sascommunity/test2.csv, Besitzername=e9782,Gruppenname=sasadmin, Zugriffsberechtigung=-rw-r--r--, Zuletzt geändert=07. Dezember 2018 09.42 Uhr, Dateigröße (Byte)=10 NOTE: 0 records were read from the infile '$HOME/sascommunity/test2.csv'. NOTE: The data set WORK.WANT2 has 0 observations and 3 variables. NOTE: DATA statement used (Total process time): real time 0.04 seconds cpu time 0.00 seconds 35 36 %if &count %then %do; 37 /* further processing of dataset */ 38 %end; 39 40 %if not &count %then %do; 41 %put empty!; empty! 42 %end;
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.