Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if a variable exists

Tags:

exists

sas

I want to test if a variable exists and if it doesn't, create it.

like image 593
Murray Avatar asked Apr 14 '11 00:04

Murray


2 Answers

The open()&varnum() functions can be used. Non-zero output from varnum() indicates the variable exists.

data try; 
    input var1 var2 var3;
    datalines;
    7 2 2
    5 5 3
    7 2 7
; 

data try2; 
    set try;
    if _n_ = 1 then do; 
        dsid=open('try'); 
        if varnum(dsid,'var4') = 0 then var4 = .; 
        rc=close(dsid);
    end;
    drop rc dsid;    
run;
like image 132
Murray Avatar answered Sep 21 '22 00:09

Murray


data try2;
    set try;
    var4 = coalesce(var4,.);
run;

(assuming var4 is numeric)

like image 36
marq Avatar answered Sep 25 '22 00:09

marq