【问题标题】:Stata : Change name of variables with values of another VariablesStata:用另一个变量的值更改变量的名称
【发布时间】:2021-04-27 09:45:26
【问题描述】:

我有一个变量数据集,如下所示: Screenshot of the Dataset.

如果可能的话,我想用与它们相关的国家的名称来标记其他变量。例如,ggdy1 是国家 1(此处为奥地利)的总债务/GDP 比率,而ggdy2 是国家 2(比利时)的总债务/GDP 比率。

为了避免从数据集到结果或命令窗口来回切换,有没有办法用合适的国家名称自动标记不同的变量(ggdypby、...)?

我的数据集中有 28 个国家,并且在 Stata 15 上工作。

【问题讨论】:

  • 您能否提供一个示例来说明您希望您的结果是什么样的?我不明白您希望如何仅使用一个国家/地区的名称来标记包含多个国家/地区数据的变量。
  • @Wouter 感谢您的评论。我想用变量 country1 的值标记变量 ggdy1,即奥地利,用变量 country2 的值​​标记 ggdy2,比利时,...
  • 其他 26 个国家呢?
  • 对于回答问题的人来说,屏幕截图比数据示例更难。请阅读 stata 标签 wiki 以获得详细建议。

标签: variables label stata


【解决方案1】:

我不得不说我认为这是一个错误的问题。您的数据结构类似于此

* Example generated by -dataex-. For more info, type help dataex
clear
input float year str7 country1 float(y1 x1) str7 country2 float(y2 x2)
1990 "Austria" 12 16 "Belgium" 20 24
1991 "Austria" 14 18 "Belgium" 22 26
end

对于大多数 Stata 目的而言,这既合乎逻辑又反常。一个简单的reshape 可以让您获得对大多数分析更有用的结构。

. reshape long country y x , i(year) j(which)
(note: j = 1 2)

Data                               wide   ->   long
-----------------------------------------------------------------------------
Number of obs.                        2   ->       4
Number of variables                   7   ->       5
j variable (2 values)                     ->   which
xij variables:
                      country1 country2   ->   country
                                  y1 y2   ->   y
                                  x1 x2   ->   x
-----------------------------------------------------------------------------

. l

     +----------------------------------+
     | year   which   country    y    x |
     |----------------------------------|
  1. | 1990       1   Austria   12   16 |
  2. | 1990       2   Belgium   20   24 |
  3. | 1991       1   Austria   14   18 |
  4. | 1991       2   Belgium   22   26 |
     +----------------------------------+

which 没有害处,但不是必需的。

附:你要求的也是可编程的,比如

foreach v of var ggdy* { 
    local suffix = substr("`v'", 5, .)
    local where = country`suffix'[1]  
    label var `v' "ggdy `where'" 
    label var pby`suffix' "pby `where'" 
    label var cby`suffix' "cby `where'" 
    label var fby`suffix' "fby `where'" 
} 
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多