需要保留当前行中出现在先前行中的缺失值

时间:2013-11-20 10:18:34

标签: sas

DATA HAVE;
INPUT POLID PRODID TOSNUM PREMID;
DATALINES;
13102100207 310 0 1
13102100207 310 0 20
13102100207 310 0 21
13102100207 310 1 1
13102100207 310 1 2
13102100207 310 1 3
13102100207 310 1 20
13102100207 310 1 21
13102100207 310 2 1
13102100207 310 2 2
13102100207 310 2 3
13102100207 310 2 20
13102100207 310 2 21
13102100207 310 3 1
13102100207 310 3 2
13102100207 310 3 20
13102100207 310 3 21
;RUN;
从上面的集合可以看出, 我有polid(连同prodid和tosnum)与不同的preid。 因为tosnum = 0我有3种不同的预售。 对于tosnum = tosnum = 1,我有5种不同的预处理,

你可以看到tosnum = 3我有4种不同的预售(premid = 3缺失)

我需要做的是, 保持先前行中存在的前提, 所以tosnum = 3将保留为premid> = 3。

我想过用set操作做一个循环(Minus和Uninon all) 但我相信有一个更好,更有效的解决方案......

你的帮助非常适合。

1 个答案:

答案 0 :(得分:0)

好的伙计们。 这就是我最终做的事情:

proc sort data=have out=have;
by polid prodid TOSNUM premid;
run;

proc sql;
select min(TOSNUM), max(TOSNUM) ,  max(TOSNUM) - min(TOSNUM)  into: min_TOSNUM, :max_TOSNUM, :diff
from have
group by polid, prodid;
quit;


%macro fictive_premid (min_TOSNUM = , diff = );
%do i =  &min_TOSNUM %to %eval(&diff-1) ;
    proc sql;
create table fictive_premid_t as
select * from 
(
select polid, prodid, %eval(&i+1) as TOSNUM, premid, 1 as FICTIVE_IND
from have
where TOSNUM = &i
except
select polid, prodid, %eval(&i+1) as TOSNUM , premid,  1 as FICTIVE_IND
from have
where TOSNUM = %eval(&i+1)
)
union 
select polid, prodid,%eval(&i+1) as TOSNUM, premid, 0 as FICTIVE_IND
from have
where TOSNUM = %eval(&i+1);
quit;

proc append base=fictive_premid data=fictive_premid_t; run;

%end;

proc sql;
insert into fictive_premid (POLID,  PRODID,  TOSNUM,  PREMID, FICTIVE_IND)
select a.*, 0 from HAVE as a
where TOSNUM = &min_TOSNUM;
quit;

%mend;


%fictive_premid (min_TOSNUM = &min_TOSNUM, diff = &diff );