如果在 Stata 的两个不同时期观察到变量值,如何生成指标

How to generate indicator if value of variable is observed in two different periods in Stata

我有一个包含各种药物及其供应日期的数据集。我想创建一个指标变量 DIBP,如果在给定年份的第 1 期和第 2 期都提供了相同的药物,则该变量的值为 1,否则为零。第一期为4月1日至6月30日,第二期为10月1日至12月31日。

我写了下面的代码:

. input id month day year str10 drug 

            id      month        day       year        drug
  1. 1 5  1 2003 aspirin
  2. 1 11 1 2003 aspirin
  3. 1 6  1 2004 aspirin
  4. 1 5  1 2005 aspirin
  5. 1 11 1 2005 aspirin
  6. end

. 
. gen date = mdy(month,day,year)

. format date %d

. 
. gen     period = 1 if inlist(month,4,5,6)
(2 missing values generated)

. replace period = 2 if inlist(month,10,11,12)
(2 real changes made)

. 
. label define plab 1"1 April to 30 June" 2"1 October to 31 December"

. label value period plab

. 
. * Generate indicator
. gen DIBP = 0

. label var DIBP "Drug In Both Periods"

. 
. bysort id year: replace DIBP = 1 if drug[period==1] == "aspirin" & drug[period==2] == "aspirin"
(0 real changes made)

. 
. list

     +---------------------------------------------------------------------------------+
     | id   month   day   year      drug        date                     period   DIBP |
     |---------------------------------------------------------------------------------|
  1. |  1       5     1   2003   aspirin   01may2003         1 April to 30 June      0 |
  2. |  1      11     1   2003   aspirin   01nov2003   1 October to 31 December      0 |
  3. |  1       6     1   2004   aspirin   01jun2004         1 April to 30 June      0 |
  4. |  1       5     1   2005   aspirin   01may2005         1 April to 30 June      0 |
  5. |  1      11     1   2005   aspirin   01nov2005   1 October to 31 December      0 |
     +---------------------------------------------------------------------------------+

我希望 DIBP 对观察值 1、2、3 和 4 取值为 1(因为他们在 2003 年和 2005 年的两个时期都服用阿司匹林),对观察值 3 取值为零(因为阿司匹林是仅在 2004 年的某个时期拍摄),但事实并非如此。我哪里错了?谢谢。

您对下标的使用显然存在问题。您似乎假设下标可用于 select 其他观察结果,这确实可以单独完成。但是您尝试的是合法的,但不是您想要的。

用作下标的表达式

period == 1 

period == 2 

会根据当前观察中period的值判断为真(1)或假(0)。然后将使用观测值 0(始终被视为具有缺失值)或观测值 1(每组观测值中的第一个)。换句话说,下标评估为观察数字,而不是定义数据的子集。

还有一个难题,因为即使对于同一个人和同一年,原则上 period 1 或 period 2 似乎也可能意味着多个观察结果。在给出的示例中,药物无论如何都是不变的,但是如果药物不同,您希望代码做什么?对我来说最明显的症结是区分某种药物的任何处方的标志和一段时间内该药物的所有处方。更多内容在 this FAQ

否则此代码可能会有所帮助。扩展到几种药物留作练习。

clear 
input id month day year str10 drug 
1 5  1 2003 aspirin
1 11 1 2003 aspirin
1 6  1 2004 aspirin
1 5  1 2005 aspirin
1 11 1 2005 aspirin
end

generate date = mdy(month,day,year)
format date %td

* code needs modification if any month is 1, 2, 3, 7, 8, 9 

generate period = 1 if inlist(month,4,5,6)
replace period = 2 if inlist(month,10,11,12)
label define plab 1"1 April to 30 June" 2"1 October to 31 December"
label value period plab

bysort id year period (date): egen all_aspirin = min(drug == "aspirin") 
by id year period: egen any_aspirin = max(drug == "aspirin") 
by id year : gen both_all_aspirin = period[1] == 1 & period[_N] == 2 & all_aspirin[1] & all_aspirin[_N]
by id year : gen both_any_aspirin = period[1] == 1 & period[_N] == 2 & any_aspirin[1] & any_aspirin[_N]

list id date drug *aspirin 

     +----------------------------------------------------------------------+
     | id        date      drug   all_as~n   any_as~n   b~ll_a~n   b~ny_a~n |
     |----------------------------------------------------------------------|
  1. |  1   01may2003   aspirin          1          1          1          1 |
  2. |  1   01nov2003   aspirin          1          1          1          1 |
  3. |  1   01jun2004   aspirin          1          1          0          0 |
  4. |  1   01may2005   aspirin          1          1          1          1 |
  5. |  1   01nov2005   aspirin          1          1          1          1 |
     +----------------------------------------------------------------------+

作为样式注释,考虑这个例子

 generate dummy = 0 
 replace dummy = 1 if frog == 42 

有经验的 Stata 程序员一般只写

 generate dummy = frog == 42 

另见 this FAQ