Help using Base SAS procedures

Missing variables cause datastep errors

Reply
Frequent Contributor
Posts: 131

Missing variables cause datastep errors

/*this dataset is to have the following ck letters, CK385,CK386,CK218,CK204

I then will merge this dataset with another as seen below and combine with a comment_code dataset

*/

data Letter_Codes_l;

set letter_codes;

run;

data ID_After_Activation_CFPB; 

merge  ID_After_Activation_CFPB (in = a) 

Letter_Codes_l (keep = ln_no  

CK385 

CK386   

CK218 

CK204 

/* The issue is if one of the variables is missing or null, it will generate the error example

ERROR: The variable CK218 in the DROP, KEEP, or RENAME list has never been referenced. In

This case the CK218 was not present in the letter_codes_l.  Is there a way I can still show all

Variable regardless.  These are to be used in totals later in the code*/

in = b) 

Comment_Codes_1st (keep = ln_no  

IP386 

CP385 

SL385 

RS385 

TR218 

MED218 

AD204

in = c) 

by ln_no; 

if a; 

run

Super User
Posts: 10,483

Re: Missing variables cause datastep errors

Besides the issue of expected variables not being there, which is a major bit in my opinion, if the number of variables isn't too large you might change to a KEEP statement for the variables not currently on your DROP list.

Or if you want to drop all of the variables starting with CK you could use CK:, or CK2: CK3: to drop all the variables that start with CK2 and CK3.

Respected Advisor
Posts: 3,777

Re: Missing variables cause datastep errors

You can use the SAS system option DKRICOND to allow the variable on the KEEP option without error.  However if you want it created when it doesn't exist that is a different story.  Perhaps those variables should actually be values of a variable and then you don't have to worry about which are missing or not.  Give more details about you what you're doing so someone can suggest a method that's not dependent on variables that might not exist.

data a;
   set sashelp.class(keep=age name x);
   run;

options dkricond=nowarn;
data a;
   set sashelp.class(keep=age name x);
   run;
proc contents data=a varnum;
  
run;
options dkricond=error;
Super User
Posts: 9,671

Re: Missing variables cause datastep errors

You can make a macro variable to contains these existed variable , and feed it to KEEP= .

Something like .Not tested.

data Letter_Codes_l;

set letter_codes;

run;

proc sql;

select name into : list separated by ' '

  from dictionary.columns

  where libname='WORK' and memname='LETTER_CODES_1' and name in ('ln_no' 'CK385' 'CK386' 'CK218' 'CK204') ;

quit;

data ID_After_Activation_CFPB;

merge  ID_After_Activation_CFPB (in = a)

Letter_Codes_l (keep = &list );

run;


Super User
Posts: 6,932

Re: Missing variables cause datastep errors

The variable is not "missing or null", it is not there at all, so if you actually need that variable, something severe happened further up in the processing chain and needs to be corrected there. If not needed, drop it from the keep list.

"miising or null" would mean that the variable is there, but does not contain any values.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 4 replies
  • 248 views
  • 0 likes
  • 5 in conversation