Skip to main content
U.S. flag
Health and Human Services Logo

An official website of the Department of Health & Human Services

menu-iconMore mobile-close-icon
mobile-back-btn-icon Back
  • menu-iconMenu
  • mobile-search-icon
AHRQ: Agency for Healthcare Research and Quality
  • Search All AHRQ Sites
  • Careers
  • Contact Us
  • Español
  • FAQs
  • Email Updates
MEPS Home Medical Expenditure Panel Survey
Font Size:
Contact MEPS FAQ Site Map  
S
M
L
XL
 

Back to HC-197I

Attachment 2:
Sample STATA Jobs for Linking Example

MEPSdofileA.log


name: <unnamed>
log: C:\Program Files\STATA\MEPSdofileA.log
log type: text
opened on: 18 Jul 2019, 13:55:12

. /* BE SURE TO UPDATE VALUES FOR CURRENT FY */
. local yr=17

. local evntnum=197

. local condnum=199

. * Get condition records coded as asthma.

. use "C:\Program Files\STATA\H`condnum'.dta", clear

. keep if ICD10CDX =="J45"
(110,530 observations deleted)

. keep CONDIDX ICD10CDX

. * Get the events linked to each of the asthma condition records.

. sort CONDIDX

. /* sample print of work.asconds - sorted by condidx;
> COND (H&condnum) records where ICD10CDX = 'J45'*/
. list if _n<=50, separator(0)

. export excel " C:\Program Files \STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. save "C:\Program Files\STATA\ASCONDS.dta", replace
file C:\Program Files\STATA\ASCONDS.dta saved

. use "C:\Program Files\STATA\H`evntnum'IF1.dta", clear

. sort CONDIDX

. label define eventype1 1 "1 MVIS"

. label define eventype1 2 "2 OPAT", add

. label define eventype1 3 "3 EROM", add

. label define eventype1 4 "4 STAZ", add

. label define eventype1 5 "5 DVIS", add

. label define eventype1 6 "6 OMED", add

. label define eventype1 7 "7 HVIS", add

. label define eventype1 8 "8 PMED", add

. label values EVENTYPE eventype1

. save "C:\Program Files\STATA\CLNK.dta", replace
file C:\Program Files\STATA\CLNK.dta saved

. keep CONDIDX EVNTIDX EVENTYPE

. merge m:m CONDIDX using "C:\Program Files\STATA\ASCONDS.dta", keepusing(CONDIDX) nogene
> rate keep(match)

Result # of obs.
not matched 0
matched 6,756

. save "C:\Program Files\STATA\ASCLNKS.dta", replace
file C:\Program Files\STATA\ASCLNKS.dta saved

. label variable CONDIDX "condidx"

. label variable EVNTIDX "evntidx"

. label variable EVENTYPE "eventype"

. /* sample print of work.asclnks - sorted by condidx;
> events linked to asthma condition records */
. list CONDIDX EVNTIDX EVENTYPE if _n<=75, sepby(CONDIDX) noobs

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. sort EVNTIDX

. /* sample print of work.asclnks - sorted by evntidx */
. list if _n<=50, separator(0)

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. keep EVNTIDX EVENTYPE

. gen first=0

. by EVNTIDX, sort: replace first=1 if _n==1
(6,744 real changes made)

. keep if first==1
(12 observations deleted)

. drop first


.

. save "C:\Program Files\STATA\ASCLNKS.dta", replace
file C:\Program Files\STATA\ASCLNKS.dta saved

. /* sample print of unique evntidxs from work.asclnks */
. list if _n<=50, separator(0)

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved


. * Get non-telephone office based visits (i.e. MVIS events) for persons with positive weights.

. use "C:\Program Files\STATA\H`evntnum'G.dta", clear

. keep EVNTIDX PERWT`yr'F SEETLKPV

. keep if PERWT`yr'F > 0 & SEETLKPV != 2
(3,595 observations deleted)

. save "C:\Program Files\STATA\MVIS.dta", replace
file C:\Program Files\STATA\MVIS.dta saved

. sort EVNTIDX

. * Identify MVIS events which were for asthma.

. keep EVNTIDX

. merge m:m EVNTIDX using "C:\Program Files\STATA\ASCLNKS.dta", nogenerate keep(match)

Result # of obs.
not matched 0
matched 1,680

. save "C:\Program Files\STATA\ASMVIS.dta", replace
file C:\Program Files\STATA\ASMVIS.dta saved

. /* sample print of work.asmvis;
> unique EVNTIDXS from work.asclnks that are non-telephone MVIS (HC-197G) events */

. list if _n<=50, separator(0)

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. * Get PMED IDs linked to the MVIS events which were for asthma.
.

. use "C:\Program Files\STATA\H`evntnum'IF2.dta", clear

. sort EVNTIDX

. save "C:\Program Files\STATA\RXLK.dta", replace
file C:\Program Files\STATA\RXLK.dta saved

. keep EVNTIDX LINKIDX EVENTYPE

. label define eventype1 1 "1 MVIS"

. label define eventype1 2 "2 OPAT", add

. label define eventype1 3 "3 EROM", add

. label define eventype1 4 "4 STAZ", add

. label define eventype1 5 "5 DVIS", add

. label define eventype1 6 "6 OMED", add

. label define eventype1 7 "7 HVIS", add

. label define eventype1 8 "8 PMED", add

. label values EVENTYPE eventype1

. label variable LINKIDX "linkidx"

. /* sample print of work.rxlk - sorted by evntidx;
> Rx+event link file records (HC-197IF2) */
. list EVNTIDX LINKIDX EVENTYPE if _n<=140, sepby(EVNTIDX) noobs

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. merge m:m EVNTIDX using "C:\Program Files\STATA\ASMVIS.dta", keepusing(EVNTIDX) nogener
> ate keep(match)
(label eventype1 already defined)

Result # of obs.
not matched 0
matched 1,418

. save "C:\Program Files\STATA\PMEDIDS.dta", replace
file C:\Program Files\STATA\PMEDIDS.dta saved

. /* sample print of work.pmedids - sorted by evntidx;
> work.rxlk records for evntidxs in work.asmvis */
. list EVNTIDX LINKIDX EVENTYPE if _n<=50, sepby(EVNTIDX) noobs

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. sort LINKIDX

. /* sample print of work.pmedids - sorted by linkidx */
. list if _n<=50, separator(0)

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. keep LINKIDX

. gen first=0

. by LINKIDX, sort: replace first=1 if _n==1
(1,371 real changes made)

. keep if first==1
(47 observations deleted)

. drop first

. save "C:\Program Files\STATA\PMEDIDS.dta", replace
file C:\Program Files\STATA\PMEDIDS.dta saved

. /* sample print of unique linkidxs in work.pmedids */
. list if _n<=50, separator(0)

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. * Get PMED records linked to MVIS events which were for asthma.
.

. use "C:\Program Files\STATA\H`evntnum'A.dta", clear

. sort LINKIDX

. *save "C:\Program Files\STATA\PMED.dta", replace

. keep LINKIDX RXRECIDX RXXP`yr'X PERWT`yr'F RXNAME

. merge m:m LINKIDX using "C:\Program Files\STATA\PMEDIDS.dta", nogenerate keep(match usi
> ng)

Result # of obs.
not matched 0
matched 3,534

. save "C:\Program Files\STATA\MVPMEDS.dta", replace
file C:\Program Files\STATA\MVPMEDS.dta saved

. sort LINKIDX

. label variable RXRECIDX "rxrecidx"

. label variable RXNAME "rxname"

. label variable RXXP`yr'X "rxxp17x"

. label variable PERWT`yr'F "perwt17f"

. /* sample print of work.mvpmeds;
> PMED (HC-197A) records for unique linkidxs in work.pmedids */
. list LINKIDX RXRECIDX RXNAME RXXP`yr'X PERWT`yr'F if _n<=200, sepby(LINKIDX) noobs

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. /* Total Rx expenditures associated with medical visits (excluding telephone) for asthma */
. tabstat RXXP`yr'X, stat(n sum) format(%15.2fc)

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. /* Total Rx expenditures associated with medical visits (excluding telephone) for asthma;
> Weighted */
. tabstat RXXP`yr'X[w=PERWT`yr'F], stat(n sum) format(%15.2fc)
(analytic weights assumed)

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

end of do-file

. exit, clear

MEPSdofileB.log


name: <unnamed>
log: C:\Program Files\STATA\MEPSdofileB.log
log type: text
opened on: 18 Jul 2019, 13:55:35

. /* BE SURE TO UPDATE VALUES FOR CURRENT FY */
. local yr=17

. local evntnum=197

. local condnum=199

. * Get condition records coded as asthma.
.

. use "C:\Program Files\STATA\H`condnum'.dta", clear

. keep if ICD10CDX =="J45"
(110,530 observations deleted)

. keep CONDIDX ICD10CDX

. * Get the events linked to each of the asthma condition records.
.

. sort CONDIDX

. /* sample print of work.asconds - sorted by condidx;
> COND (H&condnum) records where ICD10CDX = 'J45'*/
. list if _n<=50, separator(0)

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. save "C:\Program Files\STATA\ASCONDS.dta", replace
file C:\Program Files\STATA\ASCONDS.dta saved

. use "C:\Program Files\STATA\H`evntnum'IF1.dta", clear

. sort CONDIDX

. label define eventype1 1 "1 MVIS"

. label define eventype1 2 "2 OPAT", add

. label define eventype1 3 "3 EROM", add

. label define eventype1 4 "4 STAZ", add

. label define eventype1 5 "5 DVIS", add

. label define eventype1 6 "6 OMED", add

. label define eventype1 7 "7 HVIS", add

. label define eventype1 8 "8 PMED", add

. label values EVENTYPE eventype1

. save "C:\Program Files\STATA\CLNK.dta", replace
file C:\Program Files\STATA\CLNK.dta saved

. keep CONDIDX EVNTIDX EVENTYPE

. merge m:m CONDIDX using "C:\Program Files\STATA\ASCONDS.dta", keepusing(CONDIDX) nogene
> rate keep(match)

Result # of obs.
not matched 0
matched 6,756

. save "C:\Program Files\STATA\ASCLNKS.dta", replace
file C:\Program Files\STATA\ASCLNKS.dta saved

. label variable CONDIDX "condidx"

. label variable EVNTIDX "evntidx"

. label variable EVENTYPE "eventype"

. /* sample print of work.asclnks - sorted by condidx;
> events linked to asthma condition records */
. list CONDIDX EVNTIDX EVENTYPE if _n<=75, sepby(CONDIDX) noobs

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. sort EVNTIDX

. /* sample print of work.asclnks - sorted by evntidx */
. list if _n<=50, separator(0)

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. keep EVNTIDX EVENTYPE

. gen first=0

. by EVNTIDX, sort: replace first=1 if _n==1
(6,744 real changes made)

. keep if first==1
(12 observations deleted)

. drop first

. /* sample print of unique evntidxs from work.asclnks */
. list if _n<=50, separator(0)

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. rename EVNTIDX LINKIDX

. save "C:\Program Files\STATA\ASCLNKS.dta", replace
file C:\Program Files\STATA\ASCLNKS.dta saved

. * Get PMED records linked to MVIS events which were for asthma.
.

. use "C:\Program Files\STATA\H`evntnum'A.dta", clear

. sort LINKIDX

. *save "C:\Program Files\STATA\PMED.dta", replace

. keep LINKIDX RXRECIDX RXXP`yr'X PERWT`yr'F RXNAME

. merge m:m LINKIDX using "C:\Program Files\STATA\ASCLNKS.dta", nogenerate keep(match)

Result # of obs.
not matched 0
matched 10,303

. keep if PERWT`yr'F > 0
(176 observations deleted)

. save "C:\Program Files\STATA\ASPMEDS.dta", replace
file C:\Program Files\STATA\ASPMEDS.dta saved

. sort LINKIDX

. label variable RXRECIDX "rxrecidx"

. label variable RXNAME "rxname"

. label variable RXXP`yr'X "rxxp17x"

. label variable PERWT`yr'F "perwt17f"

. /* sample print of work.aspmeds;
> PMED (HC197A) records which link to condition records coded as asthma */
. list LINKIDX RXRECIDX RXNAME RXXP`yr'X PERWT`yr'F if _n<=300, sepby(LINKIDX) noobs

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. /* Total Rx expenditures associated with asthma */
. tabstat RXXP`yr'X, stat(n sum) format(%15.2fc)

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. /* Total Rx expenditures associated with asthma
> Weighted */
. tabstat RXXP`yr'X[w=PERWT`yr'F], stat(n sum) format(%15.2fc)
(analytic weights assumed)

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

end of do-file

. exit, clear

MEPSdofileC.log


name: <unnamed>
log: C:\Program Files\STATA\MEPSdofileC.log
log type: text
opened on: 18 Jul 2019, 13:57:39

. /* BE SURE TO UPDATE VALUES FOR CURRENT FY */
. local yr=17

. local evntnum=197

. local condnum=199

.
. * Get condition records coded as asthma.
.

. use "C:\Program Files\STATA\H`condnum'.dta", clear

. keep if ICD10CDX =="J45"
(110,530 observations deleted)

. keep CONDIDX ICD10CDX

. * Get the events linked to each of the asthma condition records.
.

. sort CONDIDX


. /* sample print of work.asconds - sorted by condidx;
> COND (H&condnum) records where ICD10CDX = 'J45'*/
. list if _n<=50, separator(0)

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. save "C:\Program Files\STATA\ASCONDS.dta", replace
file C:\Program Files\STATA\ASCONDS.dta saved

. use "C:\Program Files\STATA\H`evntnum'IF1.dta", clear

. sort CONDIDX

. label define eventype1 1 "1 MVIS"

. label define eventype1 2 "2 OPAT", add

. label define eventype1 3 "3 EROM", add

. label define eventype1 4 "4 STAZ", add

. label define eventype1 5 "5 DVIS", add

. label define eventype1 6 "6 OMED", add

. label define eventype1 7 "7 HVIS", add

. label define eventype1 8 "8 PMED", add

. label values EVENTYPE eventype1

. save "C:\Program Files\STATA\CLNK.dta", replace
file C:\Program Files\STATA\CLNK.dta saved

. keep CONDIDX EVNTIDX EVENTYPE

. merge m:m CONDIDX using "C:\Program Files\STATA\ASCONDS.dta", keepusing(CONDIDX) nogene
> rate keep(match)

Result # of obs.
not matched 0
matched 6,756

. save "C:\Program Files\STATA\ASCLNKS.dta", replace
file C:\Program Files\STATA\ASCLNKS.dta saved

. label variable CONDIDX "condidx"

. label variable EVNTIDX "evntidx"

. label variable EVENTYPE "eventype"

. /* sample print of work.asclnks - sorted by condidx;
> events linked to asthma condition records */
. list CONDIDX EVNTIDX EVENTYPE if _n<=75, sepby(CONDIDX) noobs

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. sort EVNTIDX

. /* sample print of work.asclnks - sorted by evntidx */
. list if _n<=50, separator(0)

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. keep EVNTIDX EVENTYPE

. gen first=0

. by EVNTIDX, sort: replace first=1 if _n==1
(6,744 real changes made)

. keep if first==1
(12 observations deleted)

. drop first

. save "C:\Program Files\STATA\ASCLNKS.dta", replace
file C:\Program Files\STATA\ASCLNKS.dta saved

. /* sample print of unique evntidxs from work.asclnks */
. list if _n<=50, separator(0)

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. * Get non-telephone office based visits (i.e. MVIS events) for persons with positive weights.
.

. use "C:\Program Files\STATA\H`evntnum'G.dta", clear

. keep EVNTIDX PERWT`yr'F SEETLKPV OBXP`yr'X

. keep if PERWT`yr'F > 0 & SEETLKPV != 2
(3,595 observations deleted)

. save "C:\Program Files\STATA\MVIS.dta", replace
file C:\Program Files\STATA\MVIS.dta saved

. sort EVNTIDX

.
. * Identify MVIS events which were for asthma.
.

. *keep EVNTIDX

. merge m:m EVNTIDX using "C:\Program Files\STATA\ASCLNKS.dta", nogenerate keep(match)

Result # of obs.
not matched 0
matched 1,680

. save "C:\Program Files\STATA\ASMVIS.dta", replace
file C:\Program Files\STATA\ASMVIS.dta saved

. /* sample print of work.asmvis;
> unique evntidxs from work.asclnks that are non-telephone MVIS (HC197G) events */
. list EVNTIDX EVENTYPE SEETLKPV OBXP`yr'X PERWT`yr'F if _n<=50, separator(0)

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. /* Total medical visit expenditures (excluding telephone) associated with asthma */
. tabstat OBXP`yr'X, stat(n sum) format(%15.2fc)

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

. /* Total medical visit expenditures (excluding telephone) associated with asthma;
> Weighted */
. tabstat OBXP`yr'X[w=PERWT`yr'F], stat(n sum) format(%15.2fc)
(analytic weights assumed)

. export excel "C:\Program Files\STATA\a1.xlsx", replace firstrow(variables)
file C:\Program Files\STATA\a1.xlsx saved

end of do-file

. exit, clear

Back to top

MEPS HOME . CONTACT MEPS . MEPS FAQ . MEPS SITE MAP . MEPS PRIVACY POLICY . ACCESSIBILITY . VIEWERS & PLAYERS . COPYRIGHT
Back to topGo back to top
Back to Top Go back to top

Connect With Us

Facebook Twitter You Tube LinkedIn

Sign up for Email Updates

To sign up for updates or to access your subscriber preferences, please enter your email address below.

Agency for Healthcare Research and Quality

5600 Fishers Lane
Rockville, MD 20857
Telephone: (301) 427-1364

  • Careers
  • Contact Us
  • Español
  • FAQs
  • Accessibility
  • Disclaimers
  • EEO
  • Electronic Policies
  • FOIA
  • HHS Digital Strategy
  • HHS Nondiscrimination Notice
  • Inspector General
  • Plain Writing Act
  • Privacy Policy
  • Viewers & Players
  • U.S. Department of Health & Human Services
  • The White House
  • USA.gov