简体   繁体   English

如何将上个月最后一个观察值复制到其他观察值?

[英]How to copy the prior month last observation's value to other observations?

In Stata在Stata中

  • Daily price data, permno is the company identifier每日价格数据, permno是公司标识符
  • For each 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条件的观察,因为mn在同一观察块中永远不相等。

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.

相关问题 如何将最后一个观测值复制到Stata面板数据中的其他观测值? - How to copy the last observation's value to other observations in panel data in Stata? 面板数据:将上一年的值赋予其他年份的所有观察值 - panel data: give value of the last year to all the observations of the other years 给定每个变量类型的一个观察值,删除所有观察值 - Deleting all observations given one observation for each variable's type 如何在Stata中进行新的观察,其所有变量的所有观察的平均值都高于它,但也忽略集合观察? - How to make a new observation in Stata that has the average of all observations above it for all variables, but also ignore set observations? 基于当前观测值和组中的其他观测值的egen中的if语句(以计算移动平均值) - if statement in egen based on current observation and other observations in group (to calculate moving average) 用先前设置的观察值替换观察值 - Replacing observations with a previous set observation 查找具有特定值的_n个观察值 - Find _n of Observation(s) that have a certain value 基于另一个观察值的逻辑 - Base a logic on another observation's value 如何使用其他观测值R或Stata填写观测值 - How to fill in observations using other observations R or Stata 如何删除上次观察值的0.1% - How to delete 0.1 percent of last observations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM