Hi,

I am facing a problem trying to rename variables. I am using Stata 13 MP.
This started with me having to reshape a long data into a wide format. Now I have unique identifiers in every row but drug variables in 350 columns. These columns are named " genericname* " and I would like to write a loop to rename them all into the respective drug they are representing. The drug names are exclusively present within columns but not in every row as some patients might not be on those drugs. I am finding it difficult to write a loop that would scan any of the entries within columns and use it to replace the variable name.

I know it is something along those coding lines but I am not quite getting it:

foreach v of varlist genericname* {
forvalues i=1/77929 {
local name = `v'[`i']
capture rename `v' `name'
}
}

Hope someone has a solution to this problem.

Thanks in advance,
Moubadda