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

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.

 

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
Onyx | Level 15
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.

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2396 views
  • 12 likes
  • 4 in conversation