I would suggest using a combination of describe
and some extended macro functions. Suppose you have two .dta files you want to compare, set1 and set2.
You could then do something along the lines of:
describe set1, varlist
local set1vars `r(varlist)'
describe set2, varlist
local set2vars `r(varlist)'
local both : list set1vars & set2vars
This will create a local macro, both
, which contains a string with the variable names that exist in both data sets. Use this macro inside of a keep
command to keep
only variables which exist in both sets.
A more thorough example would look something like:
local keeplist "make mpg foreign price"
/* Describe auto dataset */
describe using "C:/Program Files (x86)/Stata13/ado/base/a/auto.dta", varlist
local setlist1 `r(varlist)'
local keep : list keeplist & setlist1
tempfile auto
use `keep' using "C:/Program Files (x86)/Stata13/ado/base/a/auto.dta"
save `auto'
describe using "C:/Program Files (x86)/Stata13/ado/base/a/autornd.dta", varlist
local setlist2
local keep : list keeplist & setlist2
use `keep' using "C:/Program Files (x86)/Stata13/ado/base/a/autornd.dta", clear
/* Do whatever you want with now similar datasets */
* i.e.,
merge 1:1 make using `auto'
Note in the example above that you can issue describe
on the data without having it read into memory. Following from this logic, it is then quite easy to incorporate this into a loop as @Brendan Cox illustrates.
Other options involve unab
and cfvars
(available from ssc).
See a similar question here for more discussion on the topic.
isvar
(SSC) filters a namelist into a list of variables and other names. – Nick Cox