# creating new variable with existing data

#### zerostin

##### New Member
Hello,

I just started using Stata, but I am wondering if it possible to create a new variable with existing data. I have monthly data from a broker for several years. I imported one month to get to know Stata better before I go on. I started to create a new excelfile with only the variables I need. With these variables I can create the performance of investors. The formula how I would use it in excel is as follows:

Performance = (x-(y-z))/(x2-y).

I tried to do this with Stata:

Code:
gen janperformance = (x - (y - z)) / (x2 - y)
However, if I do it like this in Stata I get the error: "type mismatch R(109)".
I tried to find an answer in the FAQ on the stata website, without success. Therefore I am hoping if someone could explain if and how I can create this.

##### Ninja say what!?!
I think STATA is reading in one of the variables as character values. Try verifying that all your variables are numeric. Also, is x2 a variable, or are you trying to say x*2???

#### zerostin

##### New Member
The variables I use have the type: str12 or str11. Are these character values? If that is the case, then I made a mistake when I imported the data

x2 is a variable, not a multiplication

##### Ninja say what!?!
yes. Str most likely stands for "string" which means its a character value

#### zerostin

##### New Member
Alright, I think it works now. However, the variable type is now called "double". Is that correct? The calculation worked (I have some missing variables, but nothing really serious I guess). The output variable has the type "float". Don't know if that is normal?

Then I have another question.
I have about 60 months in separate excel files. What I want to do is create 60 new excel files with the variables I need and then import these directly into Stata. What is the best way to do this? Create a do file where I give command to use the excel files and save it to a .dta?

#### duskstar

##### New Member
I have about 60 months in separate excel files. What I want to do is create 60 new excel files with the variables I need and then import these directly into Stata. What is the best way to do this? Create a do file where I give command to use the excel files and save it to a .dta?
If you save the excel files as csv files then you can use an 'insheet' command in your do file which will read them into stata. I dont know if its possible to read 60 in at once or whether you will have to do them one at a time. As far as Im aware you'll have to do them one at a time.

With regard to your other question, it looks okay (though hard to tell without seeing if the numbers you get are what you expect, the varaible type is right). Try taking a few examples and doing the calculation by hand to ensure its doing what you want. Newer versions of Stata display string variables in a brownish colour which can help identify them.

#### zerostin

##### New Member
Thanks

I was thinking to do the calculations in Excel before I put them into stata. Saves me some time to fix that in the do file. I have a do file from a researcher and he uses the insheet command for different csv files. I hope this works for me too.
Currently I am using stata 10. Are there any advantages to use Stata 11? I can borrow the software from someone, so that isn't a problem.

#### duskstar

##### New Member
Thanks

I was thinking to do the calculations in Excel before I put them into stata. Saves me some time to fix that in the do file. I have a do file from a researcher and he uses the insheet command for different csv files. I hope this works for me too.
Currently I am using stata 10. Are there any advantages to use Stata 11? I can borrow the software from someone, so that isn't a problem.
Thats depends entirely on what you intend to use Stata for. Have a look at http://www.stata.com/stata11/ to see what was added in Stata 11.