j*******j 发帖数: 2 | 1 我是SAS的初学者,遇到一个问题,对于熟悉SAS的朋友也许很简单,特求助。
Name Order Judge
a 1 No
b 4 No
c
d 3 Yes
读取数据,如果某行缺失Order和Judge,就读取上一行的Order和Judge,如第三行c后缺
失数据,则希望读取c 4 No
向高手求助,非常感谢能出手相助。 |
g*******y 发帖数: 380 | 2 Not sure what are you saying?
For your sample data, you want result like:
c 4 No
or
a 1 No
b 4 No
c 4 No
d 3 Yes
【在 j*******j 的大作中提到】 : 我是SAS的初学者,遇到一个问题,对于熟悉SAS的朋友也许很简单,特求助。 : Name Order Judge : a 1 No : b 4 No : c : d 3 Yes : 读取数据,如果某行缺失Order和Judge,就读取上一行的Order和Judge,如第三行c后缺 : 失数据,则希望读取c 4 No : 向高手求助,非常感谢能出手相助。
|
p*****0 发帖数: 3104 | 3 you can create an array and use retain statement
it is not easy for a beginner though
【在 j*******j 的大作中提到】 : 我是SAS的初学者,遇到一个问题,对于熟悉SAS的朋友也许很简单,特求助。 : Name Order Judge : a 1 No : b 4 No : c : d 3 Yes : 读取数据,如果某行缺失Order和Judge,就读取上一行的Order和Judge,如第三行c后缺 : 失数据,则希望读取c 4 No : 向高手求助,非常感谢能出手相助。
|
j*******j 发帖数: 2 | 4 原始数据是
Name Order Judge
a 1 No
b 4 No
c
d 3 Yes
希望得到
a 1 No
b 4 No
c 4 No
d 3 Yes
就是缺失数据的,retain上一行的数据,问题很菜,谢谢大家能帮助。 |
w***y 发帖数: 114 | 5 it isnot easy.
proc sort data=old by name; run;
data new(drop=order judge rename=(judge1=judge order1=order));
set old;
by name;
retain order1 judge1;
if first.name then do;
order1=.;
judge1="";
if not first.name then do;
order1=order;
judge1=judge;
run; |
w***y 发帖数: 114 | 6 Sorry, forget two "end" to closo "do" loop. |
y****2 发帖数: 34 | 7 Hi, you may also try this one. A little bit easier:
data one;
input Name$ 1 Order 3 Judge$ 5-7;
cards;
a 1 No
b 4 No
c
d 3 Yes
;
run;
data two(drop=order judge rename=(order2=Order judge2=Judge));
set one;
retain order2 judge2;
if not missing(order) then order2=order;
if not missing(judge) then judge2=judge;
run; |
i*****o 发帖数: 88 | |
P****D 发帖数: 11146 | 9 data one;
input Name$ 1 Order 3 Judge$ 5-7;
cards;
a 1 No
b 4 No
c
d 3 Yes
;
run;
data two(keep=name order judge);
set one;
order1=lag1(order);
judge1=lag1(judge);
if order=. and Judge="" then do;
order=order1;
judge=judge1;
end;
run;
【在 i*****o 的大作中提到】 : use lag() function
|
s*****n 发帖数: 3416 | 10 You can try this:
data old;
set old;
obsid=_n_;
run;
data id_good;
set old;
if order ne .;
run;
proc sql;
/*
find out the first previous one with valid value
*/
create table match as
select a.*,max(b.obsid) as replace
from old as a left join id_good as b
on b.obsid
group by a.*;
/*
Get the value from previous valid record
*/
create table new as
select a.*,b.order as new_order
from match as a left join old as b
on a.replace=b.obsid;
quit;
data new;
set new;
if order=. then order=new_order
【在 j*******j 的大作中提到】 : 我是SAS的初学者,遇到一个问题,对于熟悉SAS的朋友也许很简单,特求助。 : Name Order Judge : a 1 No : b 4 No : c : d 3 Yes : 读取数据,如果某行缺失Order和Judge,就读取上一行的Order和Judge,如第三行c后缺 : 失数据,则希望读取c 4 No : 向高手求助,非常感谢能出手相助。
|