Bookmark and Share

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


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

st: RE: compare files via merge


From   "rasool.bux" <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: RE: compare files via merge
Date   Fri, 3 Jan 2014 11:09:12 +0000

Dear David,

I have made an ado file for generating list if id discrepancies in two data files. May be this will help you. Copy the following code and save an ado file in your working directory. Then run as:

----cmpids file1.dta file2.dta idvar----

You can change the program's drop statement if you want the list of matched ones.
Change this line
        qui drop if count_one == 1 & count_two == 1
with this one.
        qui drop if count_one ~= 1 & count_two ~= 1

Best of luck.
Rasool Bux


*********program begin ************
capture program drop cmpids
program define cmpids
        args fone ftwo idv

                if `"`fone'"' == "" | `"`ftwo'"' == "" {
                di as err "Two data files are required; nothing to do"
                exit 498
        }

                if "`fone'" == "`ftwo'" {
                        di as err "Same file is not used for structure compare"
                        exit 498
                }
        else {
                                local idvar "`idv'"
                                if substr("`fone'", -4, 4) != ".dta" local fone "`fone'.dta"
                                if substr("`ftwo'", -4, 4) != ".dta" local ftwo "`ftwo'.dta"

                qui use "`fone'", clear
                                qui contract `idvar', freq(count_one) nomiss
                                qui label variable count_one "Freq in file 1"
                                qui save freq1.dta, replace

                qui use "`ftwo'", clear
                                qui contract `idvar', freq(count_two) nomiss
                                qui label variable count_two "Freq in file 2"
                                qui save freq2.dta, replace

                                qui use freq1.dta,clear
                                qui merge 1:1 `idvar' using freq2.dta, norep
                                qui replace count_one = 0 if count_one==.
                                qui replace count_two = 0 if count_two==.
                                qui drop if count_one == 1 & count_two == 1
                                qui drop _merge
                                qui save freqtbl.dta, replace
                                qui erase freq1.dta
                                qui erase freq2.dta
                                brow `idvar' count_one count_two
                        }
end

****************** program end **********************



-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of David Kantor
Sent: Friday, January 03, 2014 1:11 AM
To: Statalist
Subject: st: compare files via merge

Hello,
Does anyone know of a program to compare files, in the spirit of -cf-, but where...
        they might not have the same number of observations
        they need to be matched on a key set of variables ?

I am using Stata 10, so I know that one possible answer is that such a program is part of standard Stata 11,12,or 13.

I was considering writing such a program, but wanted to not spend the effort if it has already been done.
(A quick search did not yield anything I thought was useful.) Thanks --David

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


________________________________

This e-mail may contain information that is privileged or confidential. If you are not the intended recipient, please delete the e-mail and any attachments and notify us immediately.


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


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