0

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)
Nick Cox
  • 35,529
  • 6
  • 31
  • 47
Anton
  • 3
  • 3
  • @TheIceBear gave a good answer. Please accept it, or otherwise explain what is unclear or not what you want. – Nick Cox Apr 05 '23 at 11:37

1 Answers1

1

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') 
}
TheIceBear
  • 2,912
  • 9
  • 23
  • The OP is well advised to filter out variables with any zeros or negative values. Several ways to do that: one is to transform conditionally on the results of `count if \`var' <= 0` being zero. – Nick Cox Apr 04 '23 at 08:11