我有三列表示中心的名称、中心的状态以及是否与中心相关联的状态。我试图看看一个中心是否是链的一部分,它只在干预状态下运行,而不是在干预状态下,或者在干预和不干预状态中。
data df;
input $ center state intervention chain;
Center1 CA 1 chain1
Center2 AZ 0 chain1
Center3 PA 0 chain2
Center4 HI 0 chain2
Center5 CA 1 chain3
Center6 CA 1 chain3;
run;做这件事最好的方法是什么?我尝试创建了三个独立的表,用于干预、不干预,这两个表都按操作状态列出了链。然后,我在每个变量中创建了虚拟变量,这些变量指示它们是在干预状态、不干预状态还是两种状态下操作。然后,我将它们合并回原来的表中,但干预/两个表之间存在重叠,以及不干预/两个表之间的重叠。
发布于 2022-05-04 21:16:43
为此,我喜欢一种非SQL解决方案;它在SQL中是可行的,但相对来说,它在它之外非常容易。
data df;
input center $ state $ intervention chain $;
datalines;
Center1 CA 1 chain1
Center2 AZ 0 chain1
Center3 PA 0 chain2
Center4 HI 0 chain2
Center5 CA 1 chain3
Center6 CA 1 chain3
;
run;
proc sort data=df;
by chain intervention;
run;
data want;
do _n_ = 1 by 1 until (last.chain); *first, check to see if it is mixed or int or nonint;
set df;
by chain intervention; *by intervention lets us easily see mixed;
length int_type $16;
if last.intervention and not last.chain then mixed=1; *if we see a row that is the last row for a by-group of intervention, but not chain, then we know this chain is mixed;
if last.chain and mixed=1 then int_type='Mixed';
else if intervention then int_type='Intervention';
else int_type='Non-Intervention';
end;
do _n_ = 1 by 1 until (last.chain); *now run through the data set a second time to put the int_type variable on the dataset;
set df;
by chain;
output;
end;
run;这是DoW回路,并遍历数据集两次,首先检查是否满足混合条件,如果满足,则标记为混合,如果不是,则根据最后记录的int_type标记它。
如果您的数据集太大,以致不需要排序,则可以通过其他方式进行排序,而无需排序,但即使使用排序,这也应该是相对较快的。如果它已经按链排序,而不是按链干预排序,那么就可以完全按照链排序,只需要更多的逻辑。
https://stackoverflow.com/questions/72119460
复制相似问题