Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: Common variables in different datasets


From   Maarten Buis <maartenlbuis@gmail.com>
To   statalist@hsphsun2.harvard.edu
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
list
*------------ end example -----------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

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


http://www.maartenbuis.nl
--------------------------

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index