【问题标题】:Replicate interpolation for multiple variables复制多个变量的插值
【发布时间】:2022-01-25 04:10:45
【问题描述】:

我在 Stata 中插入多个变量,但不知道如何有效地进行。

我的数据是这样的

Country Year v_country_1 v_country_2 v_country_3 ... v_country_234
Canada 2000 1005 1051 1052 --- 1120
Canada 2001 ---
Canada 2002 ---
Canada 2003 ---
Canada 2004 2000 3500 500 --- 10562
Korea 2000 1005 1051 1052 --- 1120
Korea 2001 ---
Korea 2002 ---
Korea 2003 ---
Korea 2004 2000 3500 500 --- 10562
... ... ... ... ... --- ....
Uganda 2000 1005 1051 1052 --- 1120
Uganda 2001 ---
Uganda 2002 ---
Uganda 2003 ---
Uganda 2004 2000 3500 500 --- 10562

如您所见,我的数据包括多个国家/地区。所以,我需要按国家/地区插入变量。

我可以使用代码很容易地插入一个变量:

by cow: ipolate v_country_1 year, gen(v_country_1_ipo)

但是,我有 234 个变量...所以几乎不可能手动复制这项工作。

你能教我怎么做吗? (我知道有一个用于复制的命令 foreach。)

【问题讨论】:

    标签: loops interpolation stata replication


    【解决方案1】:
    ds v_country_*
    
    foreach x in `r(varlist)' {
        by country: ipolate `x' year, gen(`x'_ipo)
    }
    
    // OR
    
    forvalues i = 1/234 {
        by country: ipolate v_country_`i' year, gen(v_country_`i'_ipo)
    }
    

    查看help dshelp forvalueshelp foreach 了解更多详情。

    【讨论】:

    • foreach x of var v_country_* 会起作用,因此无需调用ds
    猜你喜欢
    • 2019-09-24
    • 2018-10-18
    • 2014-09-04
    • 2020-04-29
    • 1970-01-01
    • 2013-03-28
    • 2017-11-22
    • 1970-01-01
    相关资源
    最近更新 更多