Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Common variables in different datasets

From   Maarten Buis <[email protected]>
To   [email protected]
Subject   Re: st: Common variables in different datasets
Date   Tue, 13 Sep 2011 17:30:43 +0200

2011/9/13 Nikolaos Kanellopoulos :
> I have a set of data files (file1.dta, file2.dta,...,file20.dta). There are some common variables which appear in all files and some which appear only in some. I want to create an output/table where the rows will be the variable names and the columns will be the file names and the cells will indicate whether the variable appears in each file.

*----------- begin example ----------
// create some example files
tempfile file1 file2 file3 res

sysuse auto, clear
drop mpg
save `file1'

sysuse auto, clear
drop foreign
save `file2'

sysuse auto, clear
drop mpg foreign
save `file3'

// start collecting the information
use `file1', clear
desc , replace
keep name
gen file1 = "X"
save `res'
forvalues i = 2/3 {
	use `file`i'', clear
	desc, replace
	keep name
	gen file`i' = "X"
	merge 1:1 name using `res'
	drop _merge
	save `res', replace

// display the results
use `res', clear
*------------ end example -----------
(For more on examples I sent to the Statalist see: )

Hope this helps,

Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index