Transforming all variables to log.

huangapple go评论59阅读模式
英文:

Transforming all variables to log

问题

I am using Stata, and I would like to generate a log variable based on my current available variables. However, I have too many variables and using generate command will take too much time. Is there a way to do this more efficiently?
Say, I am using:

gen log_gdp = log(gdp)

then I have to do this to every variable.

Is there a way to instead do, for every variable,

   gen log_varname = log(varname)
英文:

I am using Stata, and I would like to generate a log variable based on my current available variables. However, I have too many variables and using generate command will take too much time. Is there a way to do this more efficiently?
Say, I am using:

gen log_gdp = log(gdp)

then I have to do this to every variable.

Is there a way to instead do, for every variable,

   gen log_varname = log(varname)

答案1

得分: 1

你可以在循环中与 ds 结合使用。查看 help ds 以了解如何排除或包括不同的变量选项。我包括了 has(type numeric) 来排除所有字符串变量,因为你不能对字符串取对数。

看一下使用内置数据集 auto 的示例。

sysuse auto

ds, has(type numeric)
local variables `r(varlist)'

foreach var of local variables {
    gen log_`var' = log(`var')
}
英文:

You can use ds in combination with a loop. See help ds for options on how to exclude or include different variables. I included has(type numeric) to exclude all string variables as you cannot take the log of a string.

See this example using the built-in data set auto.

sysuse auto

ds, has(type numeric) 
local variables `r(varlist)'

foreach var of local variables {
    gen log_`var' = log(`var') 
}

huangapple
  • 本文由 发表于 2023年4月4日 09:21:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定