使用另一个变量的值更改变量名称

时间:2018-10-05 10:11:43

标签: variables stata

我正在尝试{100} rename,使用来自单独变量的值。

我有一个变量products,该变量存储有关公司销售哪些产品的信息,并使用以下方法为每个产品生成了虚拟变量:

tab products, gen(productid)

但是,变量的名称为productid1productid2,依此类推。我希望这些变量采用变量products的值。

是否可以在Stata中做到这一点而无需分别重命名每个变量?

编辑:

这是将使用的数据的示例。产品列中将有重复项。

enter image description here

然后我运行tab命令为每个产品创建一个虚拟变量以产生下表。

sort product
tab product, gen(productid)

enter image description here

我注意到它会更新标签以显示每个变量代表什么。 enter image description here

我想做的就是将值指定为变量名,例如commercial替换productid1,依此类推。

3 个答案:

答案 0 :(得分:2)

使用示例数据:

clear

input companyid str10 product
1 "P2P"
2 "Retail"
3 "Commercial"
4 "CreditCard"
5 "CreditCard"
6 "EMFunds"
end

tabulate product, generate(productid)
list, abbreviate(10)

sort product
levelsof product, local(new) clean
tokenize `new'

ds productid*

local i 0
foreach var of varlist `r(varlist)' {
    local ++i
    rename `var' ``i''      
}

产生所需的输出:

list, abbreviate(10)

     +---------------------------------------------------------------------------+
     | companyid      product   Commercial   CreditCard   EMFunds   P2P   Retail |
     |---------------------------------------------------------------------------|
  1. |         3   Commercial            1            0         0     0        0 |
  2. |         5   CreditCard            0            1         0     0        0 |
  3. |         4   CreditCard            0            1         0     0        0 |
  4. |         6      EMFunds            0            0         1     0        0 |
  5. |         1          P2P            0            0         0     1        0 |
  6. |         2       Retail            0            0         0     0        1 |
     +---------------------------------------------------------------------------+

答案 1 :(得分:2)

任意字符串可能不是合法的Stata变量名称。如果它们(a)太长,就会发生这种情况; (b)以字母或下划线以外的任何字符开头; (c)包含字母,数字和下划线以外的字符;或(d)与现有变量名称相同。您最好将字符串制成变量标签,其中只能限制80个字符。

此代码循环遍历变量并尽力而为:

gen long obs = _n 

foreach v of var productid? productid?? productid??? {
     su obs if `v' == 1, meanonly 
     local tryit = product[r(min)] 
     capture rename `v' `=strtoname("`tryit'")' 
} 

注意:代码未经测试。

编辑:这是一个测试。我添加了变量标签的代码。数据示例和代码显示了重复的值和不能为变量名的值。

clear

input str13 products
"one"
"two"
"one" 
"three"
"four"
"five"
"six something" 
end

tab products, gen(productsid) 

gen long obs = _n 

foreach v of var productsid*{
     su obs if `v' == 1, meanonly 
     local value = products[r(min)] 
     local tryit = strtoname("`value'") 
     capture rename `v' `tryit' 
     if _rc == 0 capture label var `tryit' "`value'" 
     else label var `v' "`value'" 
} 

drop obs 

describe 

Contains data
  obs:             7                          
 vars:             7                          
 size:           133                          
-------------------------------------------------------------------------------
              storage   display    value
variable name   type    format     label      variable label
-------------------------------------------------------------------------------
products        str13   %13s                  
five            byte    %8.0g                 five
four            byte    %8.0g                 four
one             byte    %8.0g                 one
six_something   byte    %8.0g                 six something
three           byte    %8.0g                 three
two             byte    %8.0g                 two
-------------------------------------------------------------------------------

答案 2 :(得分:0)

另一种解决方案是使用扩展宏功能

local varlabel:variable label

经过测试的代码是:

clear
input companyid str10 product
1 "P2P"
2 "Retail"
3 "Commercial"
4 "CreditCard"
5 "CreditCard"
6 "EMFunds"
end

tab product, gen(product_id)

* get the list of product id variables
ds product_id*

* loop through the product id variables and change the 
variable name to its label
foreach var of varlist `r(varlist)' {
local varlabel: variable label `var'
display "`varlabel'"
local pos = strpos("`varlabel'","==")+2
local varlabel = substr("`varlabel'",`pos',.)
display "`varlabel'"
rename `var' `varlabel'
}