我想通过将值推向H1来将缺失值替换为下一个变量,请参阅下面的示例。我已将所需的输出放在下面。
B'/ P>
SN OP_NAME H1 H2 H3 H4 H5
115060 NORS . 2331
115060 WIDE .
115061 .
115061 AIR . 7680
115061 ALLI .
115061 SKYW 1594
115062 NORS . .
115062 WIDE 3130 .
115063 NORS . 5414
115063 WIDE .
115064 ATLA 5231 . 11259 .
115066 ATLA 9637 . 5191 .
115067 LUXA .
115069 ATLA . 5963 .
115070 AMER 7457
115070 ATLA 10181
115070 WEST .
115072 JETS 10517
115073 SKYW . . 5515 . .
115074 MIDW .
115075 SKYW . . 4291 3499 11549
115076 DLTN 3918
输出如下:`
SN OP_NAME H1 H2 H3
115060 NORS 2331
115060 WIDE .
115061 .
115061 AIR 7680
115061 ALLI .
115061 SKYW 1594
115062 NORS . .
115062 WIDE 3130 .
115063 NORS 5414
115063 WIDE .
115064 ATLA 5231 11259
115066 ATLA 9637 5191
115067 LUXA .
115069 ATLA 5963 .
115070 AMER 7457
115070 ATLA 10181
115070 WEST .
115072 JETS 10517
115073 SKYW 5515 .
115074 MIDW .
115075 SKYW 4291 3499 11549
115076 DLTN 3918
答案 0 :(得分:4)
一个厚脸皮的双proc转置应该做的伎俩(第一个datastep是一些测试代码):
data test_code;
serial=1; h1=3; h2=.; h3=55; output;
serial=2; h1=.; h2=.; h3=32; h4=.; output;
serial=3; h1=45; h2=23; h3=.; h4=99; output;
serial=4; h1=.; h2=.; h3=5; output;
proc sort;
by serial;
run;
proc transpose data=test_code out=test_code_tran(drop=_:);
by serial;
var h:;
proc transpose data=test_code_tran prefix=h out=final_output(drop=_:);
by serial;
var col1;
where col1;
run;
如上所述,它只适用于h*
变量
答案 1 :(得分:3)
最简单的方法可能是双计数器循环。
data want;
set have;
array hs h:;
_counter=2;
do _t = 1 to dim(hs)-1 while (_counter le dim(hs));
if missing(hs[_t]) then do;
do while (missing(hs[_counter]));
_counter+1;
if _counter > dim(hs) then leave;
end;
put _t= _counter=;
if _counter le dim(hs) then do;
hs[_t] = hs[_counter];
call missing(hs[_counter]);
_counter+1;
end;
end;
end;
run;
PROC TRANSPOSE选项代码更少,更灵活;如果你有很多行,这可能会更快。
答案 2 :(得分:2)
在与Joe的类似方法中,我使用数组进行此类处理...
%LET NVARS = 5 ; data want ; set have ; array _t{&NVARS} _TEMPORARY_ ; array _n H1-H&NVARS ; t = 0 ; /* Load non-missing values into temporary array */ do i = 1 to dim(_n) ; if not missing(_n{i}) then do ; t + 1 ; _t{t} = _n{i} ; end ; end ; /* Load temporary array back into source array */ call missing(of _n{*}) ; do i = 1 to t ; _n{i} = _t{i} ; end ; drop i t ; run ;