【问题标题】:Track panel identifier name changes over time跟踪面板标识符名称随时间变化
【发布时间】:2015-11-23 21:38:52
【问题描述】:

我有一个面板标识符,它是以字母数字代码存储的公司名称,我想跟踪整个面板的名称更改。例如,公司 A 在 2001 年 5 月 25 日更名为 B。然后公司 B 在 2003 年 5 月 25 日更名为 C。然后公司 C 在 2005 年 5 月 25 日更名为 D。在这个例子中,公司 D、E、和 G 没有重命名。

我想要一个“过渡矩阵”,以便我可以查看公司 A 是否在以后以新名称参与了某些交易。例如,在测试数据的第一行,公司 A 和 G 参与了 2001 年 8 月 12 日宣布的交易。AA 变量是在很久以前确定的,所以到 2001 年 8 月 12 日左右AA 的新公司名称是 B(更改发生在 2001 年 5 月 25 日)。在第二行的测试数据中,B 和 H 参与了同样在 2001 年 8 月 12 日公布的交易。但是AA 没有时间再次更改,所以公司名称仍然是 B。

下面的代码会跟踪这些名称更改,但我怀疑这种方法过于机械化,并且对于我尚未考虑的场景并不稳健。

是否有我应该遵循的更合乎逻辑的方法或处理这种重新编码的命令?

* data on re-coding
clear
input str1 OldCode str1 NewCode str8 Date
A B 20010525
B C 20030525
C D 20050525
D   
E   
F G 20010525
G   
end 

generate temp = date(Date, "YMD")
drop Date
rename temp Date
format Date %td

* save to temp file
preserve
tempfile Codes
save "`Codes'"
restore

* merge back (recursively) to generate wide data that track re-coding
local i = 0
local j = 1

while (`j' != 0) {
    local ++i
    rename OldCode OldCode`i'
    rename NewCode NewCode`i'
    rename Date Date`i'

    cross using "`Codes'"
    count if (NewCode`i' == OldCode)
    local j = `r(N)' // zero when all re-codings accounted for
    keep if (NewCode`i' == OldCode) | missing(NewCode`i')
    replace OldCode = "" if missing(NewCode`i')
    replace NewCode = "" if missing(NewCode`i')
    replace Date = . if missing(NewCode`i')

    duplicates drop
}

* last addition is unnecessary 
drop OldCode NewCode Date

* rename and sort, only need first code
rename OldCode1 temp
drop OldCode*
rename temp OldCode
sort OldCode

* reshape to long
reshape long NewCode Date, i(OldCode)
drop _j
duplicates drop
compress
save "Codes", replace

* test data
clear
input str1 AA str1 TA str8 Date
A G 20010812
B H 20010812
C D 20050812
end
generate DealNumber = _n

generate temp = date(Date, "YMD")
drop Date
rename temp DateAnnounced
format DateAnnounced %td

clonevar OldCode = AA
joinby OldCode using "Codes.dta", unmatched(master)
drop _merge

sort DealNumber Date 
keep if (DateAnnounced >= Date) | missing(Date)
bysort DealNumber (Date) : keep if (_n == 1)

【问题讨论】:

    标签: stata recode


    【解决方案1】:

    您可以创建一个公司标识符,将同一公司的所有观察结果分组到不同的名称下。使用group_id(来自 SSC)非常简单。比如:

    * data on re-coding
    clear
    input str1 OldCode str1 NewCode str8 Date
    A B 20010525
    B C 20030525
    C D 20050525
    D   
    E   
    F G 20010525
    G
    J C 20011525
    end 
    
    * convert dyads to long form
    gen dyad_id = _n
    expand 2 if !mi(NewCode)
    bysort dyad_id: gen code = cond(_n == 1, OldCode, NewCode)
    
    * use -group_id- (from SSC) to further group dyad_id when code is the same
    clonevar jointcode = dyad_id
    group_id jointcode, match(code)
    
    * revert to the original wide form
    drop code
    bysort jointcode dyad_id: keep if _n == 1
    list, sepby(jointcode)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 2013-04-27
      • 1970-01-01
      • 2022-09-26
      • 1970-01-01
      相关资源
      最近更新 更多