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"
"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
}
"variable var2 not found
r(111);"
How may I work my way around this?
0 Response to Varying variables in datasets causes loop over files in folder to break down
Post a Comment