[英]How to copy the prior month last observation's value to other observations?
In Stata在Stata中
permno
is the company identifier每日价格数据, permno
是公司标识符permno month
, I want price end of last month对于每个permno month
,我想要上个月末的价格tsset permno date
gen m= mofd(date)
format m %tm
* price end of each month
bys permno m: gen prc_end= prc[_N]
* price end of the prior month
gen n=m-1
format n %tm
bys permno m: gen prc_endpm= prc_end if n==m[_n-1]
* no results
Current Data:当前数据:
permno date prc m prc_end n permno date prc m prc_end n
10026 24-Jan-19 145.8000031 2019m1 154.35 2018m12 10026 19 年 1 月 24 日 145.8000031 2019m1 154.35 2018m12
10026 25-Jan-19 144.5500031 2019m1 154.35 2018m12 10026 19 年 1 月 25 日 144.5500031 2019m1 154.35 2018m12
10026 28-Jan-19 140 2019m1 154.35 2018m12 10026 28-Jan-19 140 2019m1 154.35 2018m12
10026 29-Jan-19 156.8200073 2019m1 154.35 2018m12 10026 19 年 1 月 29 日 156.8200073 2019m1 154.35 2018m12
10026 30-Jan-19 150.5 2019m1 154.35 2018m12 10026 19 年 1 月 30 日 150.5 2019m1 154.35 2018m12
10026 31-Jan-19 154.3500061 2019m1 154.35 2018m12 10026 19 年 1 月 31 日 154.3500061 2019m1 154.35 2018m12
10026 01-Feb-19 154.8000031 2019m2 155.28 2019m1 10026 01-Feb-19 154.8000031 2019m2 155.28 2019m1
10026 04-Feb-19 158.4400024 2019m2 155.28 2019m1 10026 04-Feb-19 158.4400024 2019m2 155.28 2019m1
10026 05-Feb-19 158.2599945 2019m2 155.28 2019m1 10026 05-Feb-19 158.2599945 2019m2 155.28 2019m1
10026 06-Feb-19 158.2400055 2019m2 155.28 2019m1 10026 06-Feb-19 158.2400055 2019m2 155.28 2019m1
10026 07-Feb-19 156.4100037 2019m2 155.28 2019m1 10026 07-Feb-19 156.4100037 2019m2 155.28 2019m1
To make things clearer, consider a silly example dataset.为了让事情更清楚,请考虑一个愚蠢的示例数据集。
clear
set obs 6
gen permno = 1
gen date = mdy(1 + (_n > 3), real(word("1 15 31 1 15 28", _n)), 2022)
format date %td
gen m = mofd(date)
format m %tm
gen n = m - 1
format n %tm
gen price = _n
list, sepby(permno m)
+-----------------------------------------------+
| permno date m n price |
|-----------------------------------------------|
1. | 1 01jan2022 2022m1 2021m12 1 |
2. | 1 15jan2022 2022m1 2021m12 2 |
3. | 1 31jan2022 2022m1 2021m12 3 |
|-----------------------------------------------|
4. | 1 01feb2022 2022m2 2022m1 4 |
5. | 1 15feb2022 2022m2 2022m1 5 |
6. | 1 28feb2022 2022m2 2022m1 6 |
+-----------------------------------------------+
Now现在
bys permno m: gen prc_end= price[_N]
will probably work, but this would be safer:可能会起作用,但这会更安全:
bys permno m (date): gen prc_end= price[_N]
Things go wrong when you go事情 go 错了当你 go
bys permno m: gen prc_endpm= prc_end if n==m[_n-1]
The effect of the by:
is to confine calculations to blocks with the same permno
and monthly date. by:
的作用是将计算限制在具有相同permno
和每月日期的块中。 [_n-1]
here is legal but it refers to the previous observation in the same block of observations (usefully if there is one; if there isn't the code is still legal). [_n-1]
这里是合法的,但它指的是同一观察块中的前一个观察(如果有一个很有用;如果没有代码仍然是合法的)。
You want [_n-1]
to refer to the previous month (and the same permno
) but that is not what your syntax means.您希望[_n-1]
指的是上个月(以及相同的permno
),但这不是您的语法的意思。 Also, the example shows that, although your syntax is legal, there are no observations that satisfy your if
condition, as m
and n
are never equal within the same block of observations.此外,该示例表明,尽管您的语法是合法的,但没有满足您的if
条件的观察,因为m
和n
在同一观察块中永远不相等。
What you want can be done with by:
but you need to look across months.您想要的可以通过以下方式完成by:
但您需要跨月查看。
This should do it:这应该这样做:
bysort permno (m date) : gen previous = price[_n-1] if m[_n-1] == m -1
bysort permno m (date) : replace previous = previous[1]
list, sepby(permno m)
+----------------------------------------------------------+
| permno date m n price previous |
|----------------------------------------------------------|
1. | 1 01jan2022 2022m1 2021m12 1 . |
2. | 1 15jan2022 2022m1 2021m12 2 . |
3. | 1 31jan2022 2022m1 2021m12 3 . |
|----------------------------------------------------------|
4. | 1 01feb2022 2022m2 2022m1 4 3 |
5. | 1 15feb2022 2022m2 2022m1 5 3 |
6. | 1 28feb2022 2022m2 2022m1 6 3 |
+----------------------------------------------------------+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.