BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PopCorn14
Obsidian | Level 7
 
Hi,
 
Many times, I see in code that we assign a value to a return code variable like rc (see examples below) however that rc variable is never checked down the road.
 
Examples:
rc=filename ("fdir","&path");
or
%let rc=%sysfunc(grdsvc_enable(_all_,resource=SASApp));
 
Can someone explain to me the purpose of assigning rc if we do not check its value to continue?  I would expect something like this below but it is never the case.
 
If rc>0 then…..continue
Else …
 
Thank you.
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

9 REPLIES 9
SASKiwi
PROC Star

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.

jimbarbour
Meteorite | Level 14

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

PopCorn14
Obsidian | Level 7
Thank you @jimbarbour for the list of all the return codes.
PopCorn14
Obsidian | Level 7
Thank you @SASKiwi for your reply.
ballardw
Super User

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.

PopCorn14
Obsidian | Level 7

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.  

PopCorn14
Obsidian | Level 7
Thank you @ballardw for your great help. ACCEPTED SOLUTION (I need to correct that).
Kurt_Bremser
Super User

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 ....
PopCorn14
Obsidian | Level 7
Thank you @Kurt_Bremser for your explanation.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 9 replies
  • 3010 views
  • 12 likes
  • 5 in conversation