在 SAS 中,如果空变量不存在,是否有更快的方法来创建它? [英] In SAS, is there a faster way to create an empty variable if it doesn't exist?

查看:23
本文介绍了在 SAS 中,如果空变量不存在,是否有更快的方法来创建它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在使用类似于上一个问题中使用的方法,

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

测试变量是否存在

但稍作修改使其能够更轻松地处理大量变量.以下代码确保 n6 具有与 dsid2 引用的数据集相同的变量.

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;

如果我理解正确,SAS 将针对每次观察运行整个 do 循环.随着我开始使用更大的数据集,我开始感到运行时间变慢,我想知道是否有人有更好的技术?

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?

推荐答案

如果可能,最简单的方法是将常规逻辑应用于新数据集.担心以后匹配变量.完成处理后,您可以创建一个空版本的模板数据集,如下所示:

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;

然后合并 empty 和你的新数据集:

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;

在本例中,merged 数据集的 var3 值缺失.

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

这篇关于在 SAS 中,如果空变量不存在,是否有更快的方法来创建它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆