BookmarkSubscribeRSS Feed
amyk
Fluorite | Level 6

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?

4 REPLIES 4
PGStats
Opal | Level 21

Please explain... If the file is empty, it should contribute 0 records to your database when it is imported.

PG
ChrisNZ
Tourmaline | Level 20

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;

 

ballardw
Super User

Please show how you are currently importing the non-empty files.

Kurt_Bremser
Super User

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-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1908 views
  • 0 likes
  • 5 in conversation