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 at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: merging multiple files in stata


From   Steve Nakoneshny <scnakone@ucalgary.ca>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: merging multiple files in stata
Date   Thu, 22 Mar 2012 09:06:57 -0600

Negussie,

The trouble with the code you provided is that you did not save your merged dataset prior to loading your "opmnth.dta" file. So while it didn't do what you *wanted* it did do what you *told* it to do.

Here is one possible solution for you, assuming -merge- syntax from Stata 11:
---------------------------------
use "C:\UserFiles\physndata.dta", clear 
compress 
rename pp_mnth mnth
sort bene_id mnth, stable
keep bene_id mnth pp_mnthexp 
tempfile physn
save `physn'

use "C:\UserFiles\hspsdata.dta", clear 
compress 
rename hs_mnth mnth
sort bene_id mnth, stable
keep bene_id mnth hs_mnthexp
tempfile hsps
save `hsps'

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


merge 1:1 bene_id mnth using `physn'
drop _merge

merge 1:1 bene_id mnth using `hsps'

save "C:\UserFiles\mymergeddata.dta", replace

-------------------------------------------------

Steve

On 2012-03-22, at 8:17 AM, Tilahun, Negussie (CMS/CMMI) wrote:

> Hello!
> 
> 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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index