按ID和日期填充SAS中的字段

时间:2018-09-20 04:09:56

标签: macros sas retain locf

我的数据集如下:

AsAtDate  ReleaseDate ID Var1 Var2 ... Var5
200701     200601      1   x    .    
200702     200601      1   .    a
200703     200601      1   .    .
200701     200702      2   .    b 
200702     200702      2   y    b
200703     200702      2   y    .
200702     200501      3   z    .
200703     200501      3   .    .

我希望我的结果看起来像这样:

AsAtDate  ReleaseDate ID Var1 Var2 ... Var5
200701     200601      1   x    a    
200702     200601      1   x    a
200703     200601      1   x    a
200701     200702      2   .    . 
200702     200702      2   y    b
200703     200702      2   y    b
200702     200501      3   z    .
200703     200501      3   z    .

如果ReleaseDate早于AsAtDate,则如何获取数据以考虑ReleaseDate,然后按ID填充字段。

2 个答案:

答案 0 :(得分:1)

基于您的解决方案,我们可以添加CASE语句以根据需要有条件地填充数据。

proc sql;
create table want as
select *, 
       case when ReleaseDate>AsAtDate then " " 
       else max(Var1) 
       end as _Var1
from have
group by ID
order by ID, AsAtdate;
quit;

答案 1 :(得分:0)

我尝试了以下代码,是否有比这更简化的代码?

proc sql;
create table want as
select *, max(Var1) as _Var1
from have
group by ID
order by ID, AsAtdate;
quit;

data want1;
set want;
by id;
if ReleaseDate > AsAtDate then _Var1='';
run;