在后续记录中复制值的有效方法 - SAS [英] An efficient way to Copying values in subsequent records - SAS
本文介绍了在后续记录中复制值的有效方法 - SAS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个按源数据中的类别变量分组的数据集.例如:
栏 |Foo1|Foo2|Foo3酒吧2 |Foo4|Foo5|Foo6
在我导入源数据后,上述内容将导致第一个变量(父)被填充在记录 1 上,而不是 #2 和 #3.我想将父变量的值复制到记录 2 和 3 中.因此我的输出看起来更像这样:
栏 |Foo1酒吧 |Foo2酒吧 |Foo3酒吧2 |Foo4酒吧2 |Foo5酒吧2 |Foo6
我尝试使用滞后"功能,但似乎不起作用.这是我的代码:
if PARENT ~= "" then do;父母 = 父母;结尾;否则做;父母 = 滞后(父母);结尾;
解决方案
您可以保留非缺失值并将其应用于任何缺失值,例如
<前>数据要;设置有;长度最后 10 美元.;保留最后一个 '' ;如果没有丢失(PARENT),则 last = PARENT ;否则父母=最后;最后下降;跑步 ;I have a dataset that is grouped by category variables in the source data. For example:
Bar | Foo1
| Foo2
| Foo3
Bar2 | Foo4
| Foo5
| Foo6
After I import the source data, the above would result in the first Variable (Parent) being populated on Record 1 but not #2 and #3. I would like to copy the parent variable's value into record 2 and 3. Thus my output would look more like this:
Bar | Foo1
Bar | Foo2
Bar | Foo3
Bar2 | Foo4
Bar2 | Foo5
Bar2 | Foo6
I tried using a "LAG" function, but it doesn't seem to be working. Here is my code:
if PARENT ~= "" then do;
PARENT = PARENT;
end;
else do;
PARENT = LAG(PARENT);
end;
解决方案
You can retain the non-missing value and apply it to any missing values, e.g.
data want ; set have ; length last $10. ; retain last '' ; if not missing(PARENT) then last = PARENT ; else PARENT = last ; drop last ; run ;
这篇关于在后续记录中复制值的有效方法 - SAS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文