domingo, 13 de dezembro de 2020

Renaming invalid variables on Stata

After exporting an R database to Stata, the variables were like below, with "." in their names. 


Trying to rename them, resulted on the error: 
You used . in oldname, not newname. . is used in newname to indicate that the corresponding wildcard in oldname be skipped. E.g., "rename a*b* a.b*" removes what is between a and b; variable ausbvalue would be renamed abvalue. 

After searching for a solution, i found one here To rename the variables to a valid name you can just use the code below to fix the var names.

forvalues i = 1/`c(k)' {

    qui mata: st_isname(st_varname(`i')) ? 1 : st_varrename(`i', ustrtoname(st_varname(`i'))) 

}