BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14

Hello

Is it possible to tell SAS to give error when "uninitialized".

For example:

I want to get error here and I even dont see warning

Data have;
X=10;
run;

data want;
y=z*5;
Run;
1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

You can use the VARINITCHK system option, and set it to ERROR (or WARN if you only want a warning):

 

1    options varinitchk=error;
2
3    data want;
4    y=z*5;
5    Run;

ERROR: Variable z is uninitialized.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.WANT may be incomplete.  When this step was stopped there were 0 observations and 2 variables.
WARNING: Data set WORK.WANT was not replaced because this step was stopped.

 

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

What is the problem here? You can search the log for "uninitialized", just like you can search the log for "ERROR".

--
Paige Miller
Ronein
Meteorite | Level 14
I prefer that program make error and stop execute in such situation. I might miss it by looking at log
sbxkoenk
SAS Super FREQ

Use the PUTLOG statement to write errors, warning, and notes to the SAS log - The DO Loop


I produce a WARNING for your case ... but you need an input dataset (set statement) in your data step to make it work !

 

data have;
x=10;
run;

/* The macro %VAREXIST checks to see if a specific variable exists on a specified data set. */
%macro varexist(dsn=class,varname=age); 
%local dsid vnum;
%let vnum=0;
%let dsid = %sysfunc(open(&dsn));
%if &dsid %then %do;
   %let vnum = %sysfunc(varnum(&dsid,&varname));
   %let dsid = %sysfunc(close(&dsid)); 
%end;
&vnum
%mend varexist; 

data want;
 set have;
%if %varexist(dsn=have,varname=x)^=0 %then %do; 
   putlog "NOTE: x-variable is IN input dataset"; 
%end;
%if %varexist(dsn=have,varname=x) %then %do; 
 y=x*5;
%end;
run;

data want;
 set have;
%if %varexist(dsn=have,varname=z)=0 %then %do; 
   putlog "WARNING: z-variable NOT IN input dataset"; 
%end;
%if %varexist(dsn=have,varname=z) %then %do; 
 y=z*5;
%end;
run;
/* end of program */

Ciao, Koen

Quentin
Super User

You can use the VARINITCHK system option, and set it to ERROR (or WARN if you only want a warning):

 

1    options varinitchk=error;
2
3    data want;
4    y=z*5;
5    Run;

ERROR: Variable z is uninitialized.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.WANT may be incomplete.  When this step was stopped there were 0 observations and 2 variables.
WARNING: Data set WORK.WANT was not replaced because this step was stopped.

 

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.

SAS Innovate 2025: Register Today!

 

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1277 views
  • 12 likes
  • 4 in conversation