Stata:为多个观察替换虚拟 [英] Stata: replace dummy for multiple observations

查看:63
本文介绍了Stata:为多个观察替换虚拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题可能具有误导性.

我有一个带有虚拟 (dummy1) 变量的纵向数据集,该变量指示对于给定的 category 在某一年是否满足条件.我希望在接下来的二十年中也能考虑到这一事件.因此,我想创建一个新的虚拟对象 (dummy2),它在 dummy1 为 1 的观察之后的 19 个观察值以及相同的观察值取值为 1(示例如下).

I have a longitudinal dataset with a dummy (dummy1) variable indicating if a condition is met in a certain year, for given category. I want this event to be taken into account for the next twenty years as well. Hence, I want to create a new dummy (dummy2), which takes the value 1 for the 19 observations following the observation where dummy1 was 1, as well as that same observation (example below).

我试图用滞后运算符创建一个循环,但到目前为止未能使其正常工作.

I was trying to create a loop with lag operators, but failed to get it to work so far.

推荐答案

即使是失败的代码也可能接近一个好的解决方案.不提供失败的代码意味着我们无法解释您的错误.此外,关注如何使用软件做某事的问题被广泛认为是关于 SO 的边缘或题外话.

Even code that failed might be close to a good solution. Not giving code that failed means that we can't explain your mistakes. Furthermore, questions focusing on how to use software to do something are widely considered marginal or off-topic on SO.

一种方法是

bysort category (year) : gen previous = year if dummy1 
by category : replace previous = previous[_n-1] if missing(previous)
gen byte dummy2 = (year - previous) < 20 

这里的技巧是创建一个变量来保存最后一个 year 虚拟(指标)为 1,其中的技巧在 如何用之前或之后的非缺失值或序列中的缺失值替换缺失值?

The trick here is to create a variable holding the last year that the dummy (indicator) was 1, and the trick in that is spelled out in How can I replace missing values with previous or following nonmissing values or within sequences?

请注意,这独立于

  1. 面板标识符是否为数字(根据提供的证据,这里可以是字符串)

  1. whether the panel identifier is numeric (it could be string here, on the evidence given)

无论你有tsset还是xtset数据

在第一个事件之前发生了什么;在这些年里,previous 生来就缺失了,并且仍然缺失(但是,一般来说,注意时间序列末尾的代码问题).

what happens before the first event; for such years, previous is born missing and remains missing (however, in general, watch for problems with code at the ends of time series).

这篇关于Stata:为多个观察替换虚拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆