BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
BladeStorm
Calcite | Level 5

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 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
%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.

 

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User
%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.

 

BladeStorm
Calcite | Level 5

You are amazing Kurt_Bremser, your suggestion works well, thank you so much.

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

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
  • 2 replies
  • 992 views
  • 1 like
  • 2 in conversation