【问题标题】:Changing names of variables using the values of another variable使用另一个变量的值更改变量的名称
【发布时间】:2018-10-05 10:11:43
【问题描述】:

我正在尝试rename 大约 100 个虚拟变量,其值来自一个单独的变量。

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

tab products, gen(productid)

但是,变量被命名为productid1productid2 等等。我希望这些变量取而代之的是变量 products 的值。

有没有办法在 Stata 中不单独重命名每个变量?

编辑:

以下是将使用的数据示例。产品栏会有重复。

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

sort product
tab product, gen(productid)

我注意到它会更新标签以显示每个变量所代表的内容。

我想做的是将值分配为变量的名称,例如commercial,以替换productid1等。

【问题讨论】:

    标签: variables stata


    【解决方案1】:

    使用您的示例数据:

    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 |
         +---------------------------------------------------------------------------+
    

    【讨论】:

      【解决方案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
      -------------------------------------------------------------------------------
      

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-08-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-01
          • 1970-01-01
          • 2011-07-23
          • 2014-04-29
          相关资源
          最近更新 更多