2

I had a recent problem where I was using a data step to create an output file and one of the columns had been renamed. The data step executed normally filling in the now missing column with nulls without any errors or warnings. It did add a note in the log saying that a variable was undefined but otherwise there was no indication that anything was wrong.

Is there anyway to force the data step to error out or at least give a more noticeable warning in such a situation?

Ross Goddard
  • 4,242
  • 4
  • 26
  • 24
  • 2
    If you don't initialize variable, there's a Note issued with default settings. Maybe http://stackoverflow.com/questions/28328688/can-i-promote-notes-about-uninitialized-variables-to-errors/28330172#28330172 will help you. – vasja Jul 08 '15 at 17:21
  • How about using a sas macro - that seems pretty straight forward – DCR Jul 08 '15 at 17:54
  • Unfortunately I am on 9.3 so the varinitchk does not exist, but I am going to try using the dsoptions. Thanks – Ross Goddard Jul 08 '15 at 18:05

2 Answers2

1

There is an undocumented system option which turns problematic notes into errors, including the uninitialized note. I find it very handy.

1    options dsoptions=note2err;
2    data a;
3     y=x;
4    run;

ERROR: Variable x is uninitialized.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.A may be incomplete.  When this step was stopped there were 0 observations and 2 variables.
Quentin
  • 5,960
  • 1
  • 13
  • 21
  • In which versions of SAS is this option available? – user667489 Jul 08 '15 at 19:12
  • @user667489 All of them, at least all that would be currently used. – Joe Jul 08 '15 at 19:16
  • Which notes does this convert to errors? I don't doubt that it would trigger in this scenario, but I fear it might also trigger on other notes that aren't necessarily causing problems for the asker. – user667489 Jul 08 '15 at 19:36
  • @user667489 see the list that Joe just added in the answer to the question this is a duplicate of. I leave note2err on by default, and clean up anything that doesn't pass. Clean logs are worth the work. – Quentin Jul 08 '15 at 21:06
0

Put a list of the expected variables in the output file in a keep statement in your data step (or a keep= clause on the output dataset), and set option dkrocond = error; before running your data step. This is quite an old option (it goes back to at least SAS 9.1.3) so it should work in your scenario.

You can also trigger similar error messages if variables are missing from an input dataset by setting option dkricond = error;.

You can also set either of these to warn if you prefer.

Also, if you want a more general method of detecting whether a variable is present in a data set, you could try something like this:

data _null_;
    dsid = open('sashelp.class');
    vnum1 = varnum(dsid,'varname');
    vnum2 = varnum(dsid,'sex');
    rc = close(dsid);
    put vnum1= vnum2=;
run;

The crucial behaviour here is that the varnum function returns 0 for variables that are not present in the opened dataset. All of the functions used above can be used with %sysfunc, so it's even possible to do this sort of check in pure macro code, i.e. without actually running a data step or proc.

user667489
  • 9,501
  • 2
  • 24
  • 35