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: merging multiple files in stata


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   st: RE: merging multiple files in stata
Date   Thu, 22 Mar 2012 14:51:20 +0000

First, note that this is old -merge- syntax (Stata 10 and some versions earlier). The FAQ advises you to make explicit your use of any version of Stata other than the current version, namely 12.1. 

I haven't tried to track your precise logic but it does seem likely to produce incomplete results. A basic pattern to follow is: read in a dataset, -merge- with another one that you read in, -merge- with another one that you read in ... until done, but that is not what you appear to be doing here. Naturally at each stage you need to check for problems. 

Please do not send attachments to the list. In your case, please look at 

http://www.hsph.harvard.edu/cgi-bin/lwgate/STATALIST/archives/statalist.1203/date/article-928.html

to see the garbage resulting from a .p7s file (whatever that is) sent as attachment that is seen by the majority of list members. 

Nick 
[email protected] 

Tilahun, Negussie (CMS/CMMI)

I am an experienced sas programmer but I just started to work with stata.

How can I merge multiple files? I can merge two files successfully but
merging more than two becomes difficult. 
The program below run without error but I don't get the result I expected.
i.e. after I merged the third file, the output file, temp_hsps, doesn't
contain data from all the three files - it contains data only from the first
two files.  


Thanks,
Negussie 


Here is my code

clear all
set mem 500m


use "C:\UserFiles\physndata.dta", clear 
compress 
rename pp_mnth mnth sort bene_id mnth, stable
keep bene_id mnth pp_mnthexp save "C:\UserFiles\temp_physn.dta", replace

use "C:\UserFiles\hspsdata.dta", clear 
compress 
rename hs_mnth mnth sort bene_id mnth, stable
keep bene_id mnth hs_mnthexp save "C:\UserFiles\temp_hsps.dta", replace

use "C:\UserFiles\temp_physn.dta"
merge bene_id mnth using "C:\UserFiles\temp_hsps.dta"

sort bene_id mnth
drop _merge

use "C:\UserFiles\opmnth.dta", clear 
compress 
rename op_mnth mnth sort bene_id mnth save "C:\UserFiles\temp_opmnth.dta",
replace

use "C:\UserFiles\NT\suraphl\temp_opmnth.dta"
merge bene_id mnth using "C:\UserFiles\temp_hsps.dta" 
sort bene_id mnth
drop _merge

......


*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index