In some cases the function call may be confused with something else.
One example:
433 data _null_;
434 filename('somefile',"D:\Users\Owner\DATA\Super\examples.csv");
-
22
76
ERROR: Undeclared array referenced: filename.
ERROR 22-322: Syntax error, expecting one of the following: +, =.
ERROR 76-322: Syntax error, statement will be ignored.
435 run;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
436
437 data _null_;
438 rc=filename('somefile',"D:\Users\Owner\DATA\Super\examples.csv");
439 run;
In the case of FILENAME you do have a statement of the same name with different syntax. So the compiler expects certain elements depending on the syntax attempted and if you use a call that looks like the function then it expects a target variable to assign the code to for success or not.
Checking the return code is optional but I would regard it as a best practice check if following processing relies on it being successful. Not doing it is an example of lazy and potentially unreliable programming practices.
Personally I always check these if required and I've seen a lot of code examples done by others also doing this.
There are a lot of RC's in SAS. The RC's from functions, well, some matter more than others. The ones from a hash lookup, a FIND() in other words, I always check. If you're doing direct file reading FOPEN, etc., checking the RC is definitely best practice.
I also routinely check other RC's, the ones returned from Data an Procedure steps, via a macro and write them to my log at the end of a run. I find this extremely helpful.
+-----------------------------------------------------------------------------------------------------------------+
| Return codes and Sys messages as of 2021/08/12 18:09:23.73
| SYSERR = 0
| SYSCC = 0
| SYsrc=0
| SYSFILRC = 1
| SYSLIBRC = 0
| SYSLCKRC = 0
| SQLEXITCODE = 0
| SQLRC = 0
| SQLXRC = 0
| ReturnCode = 0
| Abort = 0
| SysErrorText =
| Save_ErrorText =
| SysWarningText txt =
| Save_WarningText =
+-----------------------------------------------------------------------------------------------------------------+
Jim
In some cases the function call may be confused with something else.
One example:
433 data _null_;
434 filename('somefile',"D:\Users\Owner\DATA\Super\examples.csv");
-
22
76
ERROR: Undeclared array referenced: filename.
ERROR 22-322: Syntax error, expecting one of the following: +, =.
ERROR 76-322: Syntax error, statement will be ignored.
435 run;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
436
437 data _null_;
438 rc=filename('somefile',"D:\Users\Owner\DATA\Super\examples.csv");
439 run;
In the case of FILENAME you do have a statement of the same name with different syntax. So the compiler expects certain elements depending on the syntax attempted and if you use a call that looks like the function then it expects a target variable to assign the code to for success or not.
Thank you very much @ballardw. I selected your answer for ACCEPTED SOLUTION but not sure what happened. I will try to correct that.
Your answer made a lot of sense and was well illustrated with your example.
The result of the FILENAME function is often not checked because the next step is a call to the FOPEN or DOPEN function, and that will fail if the FILENAME did not work, so the check there covers both:
rc = filename(fref,path);
fid = fopen(fref);
if fid then ....
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.