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]
RE: st: merging multiple files in stata
From
Simon Falck <[email protected]>
To
"[email protected]" <[email protected]>
Subject
RE: st: merging multiple files in stata
Date
Thu, 22 Mar 2012 15:33:14 +0000
In addition to previous answers, note that the merge-command (in stata 10<) requires that you specify the type of relationship to merge using identical "key-variables" or repeated "key-variables"
* One to one relationship
merge 1:1 id using "C:\UserFiles\hspsdata.dta"
* One to many relationship
merge 1:m id using "C:\UserFiles\hspsdata.dta"
* Many to one relationship
merge m:1 id using "C:\UserFiles\hspsdata.dta"
A suggestion is to carefully inspect your how your key-variables matches before making the matching procedure.
/Simon
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Steve Nakoneshny
Sent: den 22 mars 2012 16:07
To: [email protected]
Subject: Re: st: merging multiple files in stata
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/
*
* 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/