我需要一些帮助来创建一个新变量。我觉得egen函数是我需要使用的,但是我不能理解它。
我有3个癌症治疗变量-放射治疗,化疗和手术-每个患者接受治疗的次数。
我想创建一个新的“治疗”变量,其中1=放疗,2=化疗,3=手术,4=组合(以上3项中的1个或多个),5=无
发布于 2017-03-06 20:48:18
你应该总是展示你已经尝试过的代码,并给出示例数据。有关指导,请参阅https://stackoverflow.com/help/mcve。
假设变量radio chemo surgery的值为0或正数。
gen treatment = 5
replace treatment = 1 if radio & !chemo & !surgery
replace treatment = 2 if chemo & !radio & !surgery
replace treatment = 3 if surgery & !chemo & !radio
replace treatment - 4 if ((surgery > 0) + (radio > 0) + (chemo > 0)) > 1 使用非零为真,其否定为假的事实。请参阅this FAQ
另一种方法是:
gen treatment = 5
replace treatment = 1 if radio
replace treatment = cond(treatment == 1, 4, 2) if chemo
replace treatment = cond(inlist(treatment, 1, 2), 4, 3) if surgery 在类似的情况下,我会将none类别编码为0,而不是5。这可能会产生更合理的图形和表格。
代码未测试。
https://stackoverflow.com/questions/42625248
复制相似问题