Transforming all variables to log

236 Views Asked by At

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

There are 1 best solutions below

1
TheIceBear On BEST ANSWER

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