我有一个类似下面的数据集。一个人可以是“被动的”,“活跃的”和“活跃的”。或者'其他'。我想计算一个人被动的周数'被动'在此人变得活跃之前#39;变量' start_week'表示何时被动'法术开始。如果一个人有多个被动的'拼写,观察在下一行重复出现,但是有一个不同的start_week。
ID y_1449 y_1448 y_1449 y_1450 y_1451 y_1452 y_1501 ... y_1608 start_week
01 passive passive passive passive active active active ... 5
02 other passive active active active passive active ... 3
02 other passive active active active passive active ... 7
04
...
N other other other other passive active active ... 6
我所拥有的代码可以只用一个咒语来计算观察的持续时间,但是当观察结果再次发生时,出现问题并且两个持续时间都没有正确计算。我有以下代码:
%let period = y_1449-y_1452 y_1501-y_1553 y_1601-y_1608;
%let passive = 'passive';
%let active = 'active';
data want;
data have;
array y(*) $period;
transition_to_active=0;
weeks=0;
active_week;
do i=start_week to 62;
if y(i) in &passive. then do;
weeks=weeks+1;
end;
if y(i) in &active. and y(i+1) in &active.
and y(i+2) in &active. and y(i+3) in &active. then do;
transition_to_active=1;
active_week=i;
leave;
end;
end;
run;
此处' transition_to active'表示该人是否已被“被动”作为“被动”。活跃的'和' active_week'表示此人进行此转换的周。 任何人都可以看到为什么持续时间的计算不适用于多个法术?
答案 0 :(得分:1)
我认为这可以解决您的问题:
data input;
input ID start_week y_1447 $ y_1448 $ y_1449 $ y_1450 $ y_1451 $ y_1452 $ y_1501 $;
datalines;
01 3 passive passive passive passive active active active
02 5 other passive active active active passive active
02 7 other passive active active active passive active
;
run;
%let period = y_1447--y_1501;
%let passive = 'passive';
%let active = 'active';
data want;
set input;
array y[*] .
by ID;
last_sw = lag(start_week);
if first.id then
weeks = start_week-1;
else do;
start = last_sw;
weeks = 0;
do i=start to start_week;
if y[i]= &passive then
weeks = weeks + 1;
end;
end;
drop start last_sw i;
run;
在最后START_WEEK
开始计算循环,计算“被动”的数量,并在到达新的START_WEEK
时停止。
如果这是ID
的第一次,那么您只需要START_WEEK
减去1来获得直到开始的几周。