我有一个包含1到11事件的表,这些变量有3种字符,如HSP,REP,OH。我需要通过创建一个新的列来计算HSP,如OH1,OH2,OH3,OH4。每列的描述是
1)第一个OH之前的HSP数量 2)第1和第2 OH之间的HSP数量 3)第2和第3 OH之间的HSP数量 4)第3和第4 OH之间的HSP数量
SLN EV1 EV2 EV3 EV4 EV5 EV6 EV7 EV8 EV9 EV10 EV11
120132 HSP OH HSP HSP REP OH HSP HSP HSP OH REP
120135 OH HSP HSP HSP HSP OH HSP HSP HSP HSP OH
120200 OH HSP OH HSP HSP OH HSP OH HSP OH HSP
120251 OH HSP
120318 HSP OH HSP HSP HSP OH HSP HSP HSP HSP HSP
120327 OH HSP HSP HSP HSP OH HSP HSP HSP HSP HSP
120509 HSP OH HSP HSP HSP HSP OH HSP HSP OH HSP
120510 HSP HSP
如果没有以下' OH'然后设置为缺失E.g.120327:oh1 = 0,oh2 = 4,oh3 =。
答案 0 :(得分:3)
这是一种使用数组而不进行字符串连接和解析的方法
data counts;
input SLN EV1 $ EV2 $ EV3 $ EV4 $ EV5 $ EV6 $ EV7 $ EV8 $ EV9 $ EV10 $ EV11 $;
array EV[11] ;
array OH[4];
next = 1;
do i=1 to 4;
oh[i] = 0;
end;
do i=1 to 11;
if next > 4 then leave;
if ev[i] = "HSP" then
OH[next] = OH[next] + 1;
else if ev[i] = "OH" then
next = next + 1;
end;
datalines;
120132 HSP OH HSP HSP REP OH HSP HSP HSP OH REP
120135 OH HSP HSP HSP HSP OH HSP HSP HSP HSP OH
120200 OH HSP OH HSP HSP OH HSP OH HSP OH HSP
120251 OH HSP REP HSP REP HSP HSP HSP HSP OH HSP
120318 HSP OH HSP HSP HSP OH HSP HSP HSP HSP HSP
120327 OH HSP HSP HSP HSP OH HSP HSP HSP HSP HSP
120509 HSP OH HSP HSP HSP HSP OH HSP HSP OH HSP
120510 HSP OH HSP REP HSP HSP OH HSP HSP HSP HSP
;
run;
答案 1 :(得分:1)
这应该有效:
data want;
set have;
x1=cat(of ev1-ev11);
x2=' '||tranwrd(x1,'OH','|');
array oh oh1-oh4;
if index(x2,'|') then
do i = 1 to 4;
oh[i]=count(scan(x2,i,'|'),'HSP');
end;
else call missing(of oh1-oh4);
drop x1-x2 i;
run;
x1
将代码连接成一个长字符串。 x2
添加管道,使得扫描功能可以进行计算,如果第一个代码是oh1
'OH'
等于零
如果您需要将此数据扩展到11个ev
列和4个OH
计数之外,则可以使用数组或宏。
编辑:为OH
计数添加了数组。
如果找不到OH
,则编辑设置空值。
data counts;
input SLN EV1 $ EV2 $ EV3 $ EV4 $ EV5 $ EV6 $ EV7 $ EV8 $ EV9 $ EV10 $ EV11 $;
array EV[11] ;
array OH[4];
chk=0;
next = 1;
do i=1 to 4;
oh[i] = 0;
end;
do i=1 to 11;
if next > 4 then leave;
if ev[i] = "HSP" then
OH[next] = OH[next] + 1;
else if ev[i] = "OH" then do;
next = next + 1;
chk+1;
end;
end;
if chk<4 then do j=chk+1 to 4;
oh[j]=.;
end;
datalines;
120132 HSP OH HSP HSP REP OH HSP HSP HSP OH REP
120135 OH HSP HSP HSP HSP OH HSP HSP HSP HSP OH
120200 OH HSP OH HSP HSP OH HSP OH HSP OH HSP
120251 OH HSP REP HSP REP HSP HSP HSP HSP OH HSP
120318 HSP OH HSP HSP HSP OH HSP HSP HSP HSP HSP
120327 OH HSP HSP HSP HSP OH HSP HSP HSP HSP HSP
120509 HSP OH HSP HSP HSP HSP OH HSP HSP OH HSP
120510 HSP OH HSP REP HSP HSP OH HSP HSP HSP HSP
;
run;