In SAS, is there a faster way to create an empty v

2019-08-05 05:28发布

Currently I'm using a method similar to that used in a previous question,

Test if a variable exists

but with a small modification to make it able to handle larger numbers of variables more easily. The following code ensures that n6 has the same variables as the data set referenced by dsid2.

data n6;
set n5;
dsid=open('n5');
dsid2=open(/*empty template dataset*/);
varsn=attrn(dsid2,nvars);
i=1;
do until i = varsn;
    if varnum(dsid,varname(dsid2,i))=0 then do; 
      varname(dsid2,i)=""; 
      format varname(dsid2,i) varfmt(dsid2,i); 
     end;
    i=i+1;
end;
run;

If I understand correctly, SAS will run through the entire do loop for each observation. I'm beginning to experience slow run times as I begin to use larger data sets, and I was wondering if anyone has a better technique?

1条回答
放荡不羁爱自由
2楼-- · 2019-08-05 06:21

If possible, the simplest approach is to apply your regular logic to your new dataset. Worry about matching the variables later. When you are done with processing you can create an empty version of the template dataset like this:

data empty;
    set template(obs=0);
run;

and then merge empty and your new dataset:

data template; 
    input var1 var2 var3;
    datalines;
    7 2 2
    5 5 3
    7 2 7
; 
data empty;
    set template(obs=0);
run;

data todo;
    input var1 var2;
    datalines;
1 2
;

data merged;
    merge todo empty;
run;

In this example the merged dataset will have var3 with the value missing.

查看更多
登录 后发表回答