Hello All,
I have a CSV file contains these data, the first column is a path to a file:
"D:\SAS\testfile.txt","Windows"
"HOST.ABC.COBOL(FILE1)","Mainframe"
I have SAS code to read the CSV file and check the first column to see if the the file exist, even though the CSV file has 2 records, I only see 1 output in the log, and the output always 0.
data work.input;
length DataSet $ 50 System $ 50;
infile 'D:\SAS\inputdata.csv' end=eof dlm=',' dsd firstobs=2 truncover;
do while (not eof);
input DataSet $ MALCode $ System $;
%let dsid = %sysfunc(fileexist(DataSet));
%put &dsid;
end;
run;
In the end, if the file exist, then I want to keep the record read from CSV file, else then don't keep the record. Hope someone can help point me to the right direction.
thank you
Mike
%let dsid = %sysfunc(fileexist(DataSet));
%put &dsid;
These are macro statements, which are resolved when the macro triggers are encountered while the code is fetched for compilation. So the FILEEXIST function looks for a file named "Dataset" in the current working directory of the SAS process, which does not exist.
Use data step code:
data work.input;
length DataSet $ 50 System $ 50;
infile 'D:\SAS\inputdata.csv' dlm=',' dsd firstobs=2 truncover;
input DataSet $ MALCode $ System $;
dsid = fileexist(DataSet);
if dsid;
run;
There is also no need for the DO WHILE, the DATA step loops on itself when it has an INPUT or other statement that reads data.
This also allows use of a Subsetting IF.
%let dsid = %sysfunc(fileexist(DataSet));
%put &dsid;
These are macro statements, which are resolved when the macro triggers are encountered while the code is fetched for compilation. So the FILEEXIST function looks for a file named "Dataset" in the current working directory of the SAS process, which does not exist.
Use data step code:
data work.input;
length DataSet $ 50 System $ 50;
infile 'D:\SAS\inputdata.csv' dlm=',' dsd firstobs=2 truncover;
input DataSet $ MALCode $ System $;
dsid = fileexist(DataSet);
if dsid;
run;
There is also no need for the DO WHILE, the DATA step loops on itself when it has an INPUT or other statement that reads data.
This also allows use of a Subsetting IF.
You are amazing Kurt_Bremser, your suggestion works well, thank you so much.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.