In SAS tips & tricks #9, we looked at what happens when SAS encounters an uninitialized variable within a DATA Step. Here we look at the possibly more serious scenario of what happens when SAS cannot find a reqruied variable within a PROC Step. You will usually notice that this has happened because the log will display the following message:
“ERROR: Variable X not found”
What causes the message?
This message occurs when a PROC Step attempts to use a variable which is not present in the input dataset. This usually happens if, the variable was dropped or not kept in a preceding step, or if the code to create it has been accidentally omitted, or code has been copied from one place to another without making the necessary alterations.
What happens when this occurs?
When SAS cannot find a variable within a PROC Step, SAS will:
- outputs a message to a log stating the name of the variable(s) it cannot find, e.g. “ERROR: Variable X not found” implying that the variable X is not present in the input dataset.
- stop the execution of the current PROC Step and not produce any output.
You will notice that SAS’ behavior here is different to uninitialized variables in DATA Steps as in that instance it continues to create the output dataset.
How to resolve it?
To resolve this issue, ensure that the not found variable is available in the input dataset to the PROC Step, or udpate the PROC Step so that it does not require the not found variable in order to execute.
The following code attempts to sort the SASHELP.CLASS dataset by the variables AGE, BSA and BMI variables.
PROC SORT DATA = sashelp.class OUT=class; BY age bsa bmi; RUN;
This code results in the following messages being printed to the log.:
43 PROC SORT DATA = sashelp.class OUT=class; 44 BY age bsa bmi; ERROR: Variable BSA not found. ERROR: Variable BMI not found. 45 RUN; NOTE: The SAS System stopped processing this step because of errors. WARNING: The data set WORK.CLASS may be incomplete. When this step was stopped there were 0 observations and 0 variables.
ERROR: Variable BSA not found. ERROR: Variable BMI not found.
Tell us that the BSA and BMI variables respectively were not present in the dataset SASHELP.CLASS.
While the line:
WARNING: The data set WORK.CLASS may be incomplete. When this step was stopped there were 0 observations and 0 variables.
Tells us that as a result of the error, the output dataset was not created.
To resolve this issue, either the BSA and BMI variables should be present in the input dataset or else they should be removed from the PROC Step.
For example, here we use a DATA Step to first derive the BSA variable and then remove the BMI variable from the PROC SORT:
DATA class; SET sashelp.class; bsa = ((height *weight )/ 3131 )**1/2; RUN; PROC SORT DATA = class OUT=class2; BY age bsa ; RUN;