在SAS中如何打印超前和滞后观测值
我正在寻找一种根据标志变量打印当前、超前和滞后观测值的方法。如果某个观察值被标记,我想打印该观察值,以及它的滞后行和领先行 拥有: 想要: 我尝试了在SAS中如何打印超前和滞后观测值,sas,Sas,我正在寻找一种根据标志变量打印当前、超前和滞后观测值的方法。如果某个观察值被标记,我想打印该观察值,以及它的滞后行和领先行 拥有: 想要: 我尝试了Proc Expand函数,但由于此过程无法处理缺少的值,因此在我的工作环境中不适用 另一种方法是按照所需的顺序对数据集进行排序,然后创建一个按顺序对每行进行编号的变量——在标记条件下打印当前行+1,-1 是否有更直接的方法根据标志实现由当前行、滞后行和前导行组成的数据集 对于flag=,可以使用var1+1和var-1。如下图所示 pro
Proc Expand
函数,但由于此过程无法处理缺少的值,因此在我的工作环境中不适用
另一种方法是按照所需的顺序对数据集进行排序,然后创建一个按顺序对每行进行编号的变量——在标记条件下打印当前行+1,-1
是否有更直接的方法根据标志实现由当前行、滞后行和前导行组成的数据集 对于flag=,可以使用var1+1和var-1。如下图所示
proc sql;
create table want as
select distinct a.* from
(select * from have)a
inner join
(select * from have
where flag = 1 )b
on a.var1 = b.var1
or a.var1 = b.var1-1
or a.var1 = b.var1 +1;
使用LAG()函数很容易找到FLAG的上一个值。找到下一个值有点困难。这里有一个方法
data want ;
set have end=eof;
by id ;
if not eof then set have(keep=flag rename=(flag=lead_flag) firstobs=2);
lag_flag=lag(flag);
if first.id then call missing(lag_flag);
if last.id then call missing(lead_flag);
if flag or lead_flag or lag_flag ;
run;
更一般的情况是
- 在每个组中,报告满足fulcral条件的每一行以及前后的p行和q行
data have;
do id = 'A', 'B', 'C', 'D';
do seq = 1 to 36;
rowid + 1;
x = seq;
if id = 'D' then x = floor(12*ranuni(123));
flag = mod(x,7);
output;
end;
end;
run;
%let lagsize = 2;
%let leadsize = 3;
data want;
if _n_ = 1 then do;
declare hash marks();
retain __n .;
marks.defineKey('__n');
marks.defineData('__n');
marks.defineDone();
end;
marks.clear();
do __n = 1 by 1 until(last.id);
set have;
by id;
if
flag=0 /* fulcrum criteria checked for each row in group */
then do;
* computation is "store sequence number that would fall in the lag lead 'about' space";
_n_ = __n;
do __n = _n_ - &lagsize to _n_ + &leadsize;
marks.replace();
end;
__n = _n_;
end;
end;
do __n = 1 to __n;
set have;
if marks.check()=0 then output; * evaluate for each row in group;
end;
run;
该技术可以扩展到根据不同的fulcral条件需要不同“大约”尺寸的情况
data want ;
set have end=eof;
by id ;
if not eof then set have(keep=flag rename=(flag=lead_flag) firstobs=2);
lag_flag=lag(flag);
if first.id then call missing(lag_flag);
if last.id then call missing(lead_flag);
if flag or lead_flag or lag_flag ;
run;
data have;
do id = 'A', 'B', 'C', 'D';
do seq = 1 to 36;
rowid + 1;
x = seq;
if id = 'D' then x = floor(12*ranuni(123));
flag = mod(x,7);
output;
end;
end;
run;
%let lagsize = 2;
%let leadsize = 3;
data want;
if _n_ = 1 then do;
declare hash marks();
retain __n .;
marks.defineKey('__n');
marks.defineData('__n');
marks.defineDone();
end;
marks.clear();
do __n = 1 by 1 until(last.id);
set have;
by id;
if
flag=0 /* fulcrum criteria checked for each row in group */
then do;
* computation is "store sequence number that would fall in the lag lead 'about' space";
_n_ = __n;
do __n = _n_ - &lagsize to _n_ + &leadsize;
marks.replace();
end;
__n = _n_;
end;
end;
do __n = 1 to __n;
set have;
if marks.check()=0 then output; * evaluate for each row in group;
end;
run;