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 2025: Call for Content

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 283 views
  • 1 like
  • 2 in conversation