Check If The CSV File Has Data

Quick Guide: Verify Files Before Data Import

Importing data from CSV files is a common task in SAS. However, sometimes CSV files may not have any data, which can cause issues when importing them. Here are the steps to check if a CSV file has data and import it into SAS:

Check if the CSV file has data

To check if a CSV file has data, you can use the SAS data step to look at the file. Use the LIST statement to dump the lines read to the SAS log. You can use the OBS= option of the INFILE statement to limit the number of lines read. Example:

filename myfile '/home/subhroster20070/inputs/class.csv';
data _null_;
infile myfile obs=10;
input;
list;
run;
Check if the CSV file has data

Using Filexists function to check csv file.

Use the FILEEXIST function: The FILEEXIST function checks if a file exists and returns a value of 1 if it does and 0 if it doesn’t. You can use this function to check if a CSV file has data. Example:

option mlogic mprint;
%let filename='/home/subhroster20070/inputs/classs.csv';
%macro findfile;                                 
  %if %sysfunc(fileexist(&filename)) %then 
  	%do;
   %put The file &filename exists.; 
   %end;
  %else %put WARNING: The file &filename does not exist. ;
%mend;                                         
    
/* invoke the macro */ 
%findfile;

Here is the findfile macro used with the IFC function.

option mlogic mprint;
%let filename='/home/subhroster20070/inputs/class.csv';

%macro findfile;
	%let msg=%sysfunc(ifc(%sysfunc(fileexist(&filename.)),
	%nrstr(%Put NOTE: filename &filename. exists;)
,%nrstr(%Put WARNING: not exist &filename.;endSAS;) ));
%mend;

/* invoke the macro */ 
%findfile;

Macro to check multiple csv file and import of the file exists.

The macro that I have written is designed to check for multiple CSV files in a specified directory and then import them one by one.

This is a handy code snippet for anyone who needs to work with large amounts of data regularly. By automating the process of importing the files, it saves time and reduces the risk of errors.

%macro importF(outf);
	%let filrf=myfile;
	%if %sysfunc(fileexist(&outf)) %then
		%do;
			%let rc=%sysfunc(filename(filrf, &outf));
			%let fid=%sysfunc(fopen(&filrf));
			%if &fid > 0 %then
				%do;
					%let rc=%sysfunc(fread(&fid));
					%let rc=%sysfunc(fget(&fid, mystring));
					%if &rc=0 %then %do;
						%put &mystring;
						 %import_csv(&outf.);
					%end;
					%else
						%put &outf file is empty;
					%let rc=%sysfunc(fclose(&fid));
				%end;
			%let rc=%sysfunc(filename(filrf));
		%end;
	%else
		%put &outf file does not exist;
%mend importF;


%macro import_csv(file);
proc import
  datafile="&file."
  out=%scan(%scan(&file,-1,'/'),1,'.')
  dbms=csv
  replace
;
run;
%mend;

To use this macro, you can write the filenames in a datalines statement and execute the macro using call execute function.

data _null_;
input files :$50.;
call execute(cats('%nrstr(%test(',files,'))'));
datalines;
/home/subhroster20070/inputs/class.csv
/folders/myfolders header_with_data.csv
/home/subhroster20070/inputs/cars.csv
;
run;

Conclusion 

By following the methods outlined in this blog post, you can ensure that your CSV files are imported correctly into SAS. Checking if a CSV file has data and importing it into SAS is a straightforward process. By using these methods, you can ensure that your CSV files are imported correctly into SAS. Additionally, checking multiple CSV files for existence in SAS can save time and reduce errors. By using these methods, you can efficiently manage large numbers of raw data files.

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.