Wednesday, April 1, 2009

How to check if the File is exist or not in SAS

How to check if the File is exist or not in SAS:

Guys… Let me try explaining how to check if the file exist in the library or directory using SAS.

Here I am writing a macro to check if the file exist in the directory or not.

Here is the way to check it…

%macro check(dir= ) ;
%if %sysfunc(fileexist(&dir)) %then %do;
%put File Exists ;

%else %do ;
%put File doesn’t exist.;
%end ;

%mend ;
%check(dir="C:\Documents and Settings\sannapareddy\Desktop\Holidays 2009.docx");

%sysfunc option empowers me to use the same Data step functions outside the Data step. Almost all of the Data step functions are available to use outside the data step if we use %sysfunc.

fileexist option, verifies the existence of an external file. It can also verifies the SAS library.

Here is another simple technique to check.....

data _null_;
x=filexist('C:\Documents and Settings\sannapareddy\Desktop\Holidays 2009.docx');
call symput('myvar1',x);

%put &myvar1;

Call symput is used to create a macro variable( myvar1)with the variable 'X' value assigned to it.

See the log for details,.... if the value you see in log is one ... then the file exists, if the value in the log is 0 the the file doesn't.


Samba said...

I have known the path of a folder but i don't know how many files are there in that folder and even don't know what kind of files they are (i mean .xls, .csv, .txt etc). can anybody tell me how to find out the all files in a folder?

sarath said...

DOPEN funstion will let you count number of files present in the given directory (Folder)....

Here is how ....

filename dir 'D:\Documents and Settings\annapareddys\Desktop\STUDY\FOLDER';
%let dirid = %sysfunc(DOPEN(dir));
%let dircnt = %sysfunc(DNUM(&dirid));
%put &dircnt;


Post a Comment