向后 select 第一个非负数 SAS
Going backwards select the first non negative number SAS
我有一个类似于以下的数据集:
ID COL70 COL71 COL72 COL73 COL74
1 4 3 2 -998 .
2 2 0 2 1 -998
3 1 -998 -998 . .
4 3 4 -998 -998 -998
我想要做的是有一个新列,我们称之为 NEWCOL,它具有从 COL74 向后看的第一个非负值,因此它看起来如下:
ID COL70 COL71 COL72 COL73 COL74 NEWCOL
1 4 3 2 -998 . 2
2 2 0 2 1 -998 1
3 1 -998 -998 . . 1
4 3 4 -998 -998 -998 4
我在 WPS 工作,所以这需要在 SAS 或 PROC SQL 中。
您可以像这样遍历数组。
data have;
array c[*] col70-col74;
input id c[*];
do i = dim(c) to 1 by -1 until(sign(c[i]) eq 1);
newcol=c[i];
end;
if i eq 0 then newcol=.;
cards;
1 4 3 2 -998 .
2 2 0 2 1 -998
3 1 -998 -998 . .
4 3 4 -998 -998 -998
5 -3 -4 -998 -998 -998
;;;;
run;
proc print;
run;
我为任何想要相同的人想出了它:
data want;
set have;
temp = 0;
num = 74;
array col{70:74} col70-col74;
do while (temp = 0);
if col{num} >= 0 then do;
newvar = col{num};
temp = 1;
end;
else num = num - 1;
end;
run;
在SQL中一个简单的case
表达式可以'backup'得到想要的结果
proc sql;
create table want as
select have.*
,
case
when col74 > 0 then col74
when col73 > 0 then col73
when col72 > 0 then col72
when col71 > 0 then col71
when col70 > 0 then col70
else .
end as last_positive
from have
;
quit;
我有一个类似于以下的数据集:
ID COL70 COL71 COL72 COL73 COL74
1 4 3 2 -998 .
2 2 0 2 1 -998
3 1 -998 -998 . .
4 3 4 -998 -998 -998
我想要做的是有一个新列,我们称之为 NEWCOL,它具有从 COL74 向后看的第一个非负值,因此它看起来如下:
ID COL70 COL71 COL72 COL73 COL74 NEWCOL
1 4 3 2 -998 . 2
2 2 0 2 1 -998 1
3 1 -998 -998 . . 1
4 3 4 -998 -998 -998 4
我在 WPS 工作,所以这需要在 SAS 或 PROC SQL 中。
您可以像这样遍历数组。
data have;
array c[*] col70-col74;
input id c[*];
do i = dim(c) to 1 by -1 until(sign(c[i]) eq 1);
newcol=c[i];
end;
if i eq 0 then newcol=.;
cards;
1 4 3 2 -998 .
2 2 0 2 1 -998
3 1 -998 -998 . .
4 3 4 -998 -998 -998
5 -3 -4 -998 -998 -998
;;;;
run;
proc print;
run;
我为任何想要相同的人想出了它:
data want;
set have;
temp = 0;
num = 74;
array col{70:74} col70-col74;
do while (temp = 0);
if col{num} >= 0 then do;
newvar = col{num};
temp = 1;
end;
else num = num - 1;
end;
run;
在SQL中一个简单的case
表达式可以'backup'得到想要的结果
proc sql;
create table want as
select have.*
,
case
when col74 > 0 then col74
when col73 > 0 then col73
when col72 > 0 then col72
when col71 > 0 then col71
when col70 > 0 then col70
else .
end as last_positive
from have
;
quit;