Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables Stata:使用其他变量的值更改变量名称_Variables_Label_Stata - Fatal编程技术网

Variables Stata:使用其他变量的值更改变量名称

Variables Stata:使用其他变量的值更改变量名称,variables,label,stata,Variables,Label,Stata,我有一个如下所示的变量数据集: 如果可能的话,我想用与之相关的国家名称来标记其他变量。例如,ggdy1是国家1(此处为奥地利)的总债务/GDP比率,而ggdy2是国家2(比利时)的总债务/GDP比率 为了避免从数据集到结果或命令窗口的来回转换,是否有一种方法可以自动将不同的变量(ggdy,pby,…)标记为合适的国家/地区名称 我的数据集中有28个国家,正在研究Stata 15。我不得不说,我认为这是一个错误的问题。您的数据结构与此类似 * Example generated by -data

我有一个如下所示的变量数据集:

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

为了避免从数据集到结果或命令窗口的来回转换,是否有一种方法可以自动将不同的变量(
ggdy
pby
,…)标记为合适的国家/地区名称


我的数据集中有28个国家,正在研究Stata 15。

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

* 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 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 |
     +----------------------------------+
它不会造成伤害,但不是必需的

另外,你要求的也是可编程的,比如

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'" 
} 
    

你能提供一个你希望你的结果是什么样的例子吗?我看不出您想如何用一个国家的名称来标记包含多个国家数据的变量。@谢谢您的评论。我希望变量ggdy1标记为变量country1的值,即奥地利,ggdy2标记为变量country2的值,比利时,……其他26个国家的情况如何?屏幕截图比数据示例更难回答。请阅读
stata
tagwiki获取详细建议。