Hi list,

I have a number of datasets for different years in a folder - the files are called data2000, data2001 etc.
I'm trying to append all of the datasets using the following command (2010 being the last year I have data)

Code:
clear all
cd "folder where I have the data"
use data2000
gen year = 2000
foreach num of numlist 2000/2010 {
append using data`num'
replace year = `num' if year==.
}
save "name of combined data set"
Now, this works well as long as the variables are stored identically in the different files.. However, in some files a certain variable, "var1", is byte/long, and in some it's string. This gives me the following error:

"variables var1 is str7 in master, but long/byte in using data
You could specify append's force option to ignore this string/numeric mismatch. The using variable would then be treated as if it contained "".
r(106);"

So, I try to counter the problem with another loop, that I run before the previous. After having fixed var1, I run the first loop again. However, it breaks down because var2 is posing the same problem, so I add var 2 to the loop and run it again.

Code:
cd "folder where I have the data"
local allies: dir . files "data*.dta", respectcase
foreach file of local allfies {
use `"`file'"', clear
tostring var1, replace
tostring var2, replace
gettoken filename: file, parse(".")
save `"filename'.dta"', replace
}
Here, my problem occurs. If var2 is not in all the datasets, then the loop won't work, and gives me the following error:
"variable var2 not found
r(111);"

How may I work my way around this?