Pregunta

I would like to replace and retain the original variable name after using the encode command for a set of variables x1-xn. This is where I've started:

for var x*: encode x*, generate(x*_) label(label)

My question is: what is a clean way to drop the x* (original string) variables?

When I figure that out, I can execute this command to rename x*_ (the new encoded variable):

rename x*_ x*
¿Fue útil?

Solución

Maybe you meant something like:

clear all
set more off

* example database
sysuse auto
keep make

clonevar make2 = make

describe
list in 1/5, nolabel

* what you want
foreach v of varlist make* {
    encode `v', gen(new`v')
    drop `v'
    rename new`v' `v'
}

describe
list in 1/5, nolabel

Translated into plain English (although code is straightforward) this is: for each variable that starts with make, encode it generating a new variable, then drop the old one and rename the new one. Local macros are used. See help foreach and help macro for details.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top