0

I want to number/index observations in my Stata dataset by date with the following logic: if the eventdate = observation date --> assign index (or counting number) 0, give n-1 n-2 ... for the previous dates and n+1 , n+2 .... for the following dates.

I checked the help but I couldn't find a convincingly helpful answer.

4

2 回答 2

1

您没有定义 n,但使用与 @Metrics 相同的猜测,您要求的变量只是

  . gen diff = cond(current_date == event_date, 0, current_date) 
于 2013-07-15T17:42:17.063 回答
0

这不是有效的方法;但我认为这应该有效:

use http://dss.princeton.edu/training/tsdata.dta
gen date1=substr(date,1,7)
gen datevar=quarterly(date1,"yq")
format datevar %tq
browse date date1 datevar
tsset datevar
gen time=_n


gen index=0 
*say your event date is 1996q1
replace index=time if tin(1957q1,1995q4)|tin(1996q2,2005q1)


list datevar time index gdp in 150/160
gdp datevar time    index
.5369373    1994q2  150 150
.6064236    1994q3  151 151
-.0578999   1994q4  152 152
.0906607    1995q1  153 153
.3216962    1995q2  154 154
1.020297    1995q3  155 155
.4759386    1995q4  156 156
.5014071    1996q1  157 0
1.115691    1996q2  158 158
.2851675    1996q3  159 159
1.187331    1996q4  160 160
于 2013-07-15T16:31:46.410 回答