英文:
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')
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论