# renaming multiple variables

#### xbender

##### New Member
Hi,

I have data in a "long" format refering to sales and profits of more products. When I reshape the data to "wide", I end up with more variables "profit" and "sales" that have some ID number refering to what product the variable is connected to.

Can I rename all variables having eg. ID=1234 in the name, so that both "profit1234" and "sales1234" would be renamed?

subquestion: when I use "summary" command, since a lot of my variables are quite long in their name, I often end up having shortening "~" in the variable name. How can I get the full name of the variable to show?

thank you!

#### xbender

##### New Member
ok, so I have a commandlist like this:

Code:
gen exp_share9401000=exp9401000/expTotal
gen exp_share9401870=exp9401870/expTotal
gen exp_share9402500=exp9402500/expTotal
gen exp_share9402600=exp9402600/expTotal
gen exp_share9405400=exp9405400/expTotal
gen exp_share9408160=exp9408160/expTotal
gen exp_share9410100=exp9410100/expTotal
gen exp_share9410200=exp9410200/expTotal
gen exp_share9450850=exp9450850/expTotal
gen exp_share9451200=exp9451200/expTotal
gen exp_share9451000=exp9451000/expTotal
and I would like to loop this list of commands. The problem is I dont know how to refer to numbers.
lets say:

Code:
foreach exp in exp1 exp2 .. expn{
gen exp_share?=exp?/expTotal
}
but instead of "?" I feel like I should use some symbol to tell stata that a number follows... how to loop the command list?
thank you!

#### xbender

##### New Member
similarly I feel like for handling commands like this (it creates a scalar, that is a result of summing multiples of average price and expenditure share across different products):

Code:
scalar define index= exp_share9401000* avgp9401000+ exp_share9401870* avgp9401000+ exp_share9402500* avgp9402500+ exp_share9402600* avgp9402600+ exp_share9405400* avgp9405400+ exp_share9408160* avgp9408160+ exp_share9410100* avgp9410100+ exp_share9410200* avgp9410200+ exp_share9450850* avgp9450850+ exp_share9451200* avgp9451200+ exp_share9451000* avgp9451000
there has to be a easier way to write them...
I would be very glad for any tips...

#### divishoot

##### New Member
Hi,

subquestion: when I use "summary" command, since a lot of my variables are quite long in their name, I often end up having shortening "~" in the variable name. How can I get the full name of the variable to show?

thank you!
i think u don't have the option to do this in sum but with des u can write
des, fullnames
since you have a lot of variables u can do this:

foreach v of varlist _all {
describe v', fullnames
summarize v'
}

I'm pretty sure there should be a better way to do this

#### divishoot

##### New Member
the loop is fine- this is pretty much the same

foreach v in exp1 exp2 exp3{
g sharev'=v'/ expTotal
}

if you don't want to specify the whole list then this will work:
foreach v of varlist exp*{
g sharev'=v'/ Totalexp
}
but notice i changed the name of the total since this too would meet the exp* criteria