Anthony Martin
Anthony Martin

Reputation: 787

SAS : keep if exists

I have several databases, one per geographical variables, that I want to append in the end. I am doing some data steps on them. As I have large databases, I select only the variables I need when I first call each table. But on tables in which one variable always equals 0, the variable is not in the table.

So when I select my (keep=var) in a for loop, it works fine if the variable exists, but it produces an error in the other case, so that these tables are ignored.

%do i=1 to 10 ; 

data temp;
    set area_i(keep= var1 var2);
run;

proc append base=want data=temp force; 
run;

%end;

Is there a simple way to tackle that ?

Upvotes: 2

Views: 2052

Answers (2)

dwjohnston
dwjohnston

Reputation: 11812

How about just adding it to the table if it doesn't already exist?

/*look at dictionary.columns to see if the column already exists*/
proc sql;
    select name into :flag separated by ' ' from dictionary.columns where libname = 'WORK' and memname = 'AREA_I' and name = 'VAR1';
run;

/*if it doesn't, then created it as empty*/
%if &flag. ne VAR1 %then %do;
    data area_i;
        set area_i;
        call missing(var1);
    run;    
%end;

Upvotes: 1

Anthony Martin
Anthony Martin

Reputation: 787

In fact I have found a solution : the DKRICOND (or DKROCOND) options specify the level of error detection to report when a variable is missing from respectively an input (or output) data set during the processing of a DROP=, KEEP=, or RENAME= data set option.

The options are DKRICOND=ERROR | WARN | WARNING | NOWARN | NOWARNING, so you just wave to set

dkricond=warn

/*your program, in my case :*/
%do i=1 to 10 ; 
data temp;
    set area_i(keep= var1 var2);
run;
proc append base=want data=temp force; 
run;
%end;

dkricond=error /* the standard value, probably better to set it back after/ */

Upvotes: 1

Related Questions