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-206I

Attachment 2:
Sample STATA Jobs for Linking Example

MEPSdofileA.log


name: <unnamed>
log: C:\Program Files\ STATA\Stata2020\StataDoFileA.log
log type: text
opened on: 20 Jul 2020, 14:15:07

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

. local evntnum=206

. local condnum=207

. * Get condition records coded as asthma.
.

. use “C:\Program Files\ STATA\Stata2020\H`condnum'.dta”, clear

. keep if CCSR1X == “RSP009” & CCSR2X == “-1” & CCSR3X == “-1”
(92,815 observations deleted)

. keep CONDIDX CCSR1X CCSR2X CCSR3X 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 CCSR1X = 'RSP009' and CCSR2X = '-1' and CCSR3X = '-1'*/
. list if _n<=50, separator(0)

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

. save “ C:\Program Files \STATA\Stata2020\ASCONDS.dta”, replace
file C:\Program Files\ STATA\Stata2020\ASCONDS.dta saved

. use “C:\Program Files\ STATA\Stata2020\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 7 “7 HVIS”, add

. label define eventype1 8 “8 PMED”, add

. label values EVENTYPE eventype1

. save “ C:\Program Files\STATA\Stata2020\CLNK.dta”, replace
file C:\Program Files\ STATA\Stata2020\CLNK.dta saved

. keep CONDIDX EVNTIDX EVENTYPE

. merge m:m CONDIDX using “C:\Program Files\ STATA\Stata2020\ASCONDS.dta”, keepusing(CONDIDX) nogenerate keep(mat > ch)

Result # of obs.

not matched 0
matched 8,854

. save “C:\Program Files\ STATA\Stata2020\ASCLNKS.dta”, replace
file C:\Program Files\ STATA\Stata2020\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\Stata2020\a1.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\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\Stata2020\a1.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\a1.xlsx saved

. keep EVNTIDX EVENTYPE

. gen first=0

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

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

. drop first

. save “C:\Program Files\ STATA\Stata2020\ASCLNKS.dta”, replace
file C:\Program Files\ STATA\Stata2020\ASCLNKS.dta saved

. /* sample print of unique evntidxs from work.asclnks */

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

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

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

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

. keep EVNTIDX PERWT`yr'F

. keep if PERWT`yr'F > 0
(2,787 observations deleted)

. save “C:\Program Files\ STATA\Stata2020\MVIS.dta”, replace
file C:\Program Files\ STATA\Stata2020\MVIS.dta saved

. sort EVNTIDX

. * Identify MVIS events which were for asthma.
.

. keep EVNTIDX

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

Result # of obs.

not matched 0
matched 1,565

. save “C:\Program Files\ STATA\Stata2020\ASMVIS.dta”, replace
file C:\Program Files\ STATA\Stata2020\ASMVIS.dta saved

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

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

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

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

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

. sort EVNTIDX

. save “C:\Program Files\ STATA\Stata2020\RXLK.dta”, replace
file C:\Program Files\ STATA\Stata2020\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-206IF2) */
. list EVNTIDX LINKIDX EVENTYPE if _n<=140, sepby(EVNTIDX) noobs

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

. merge m:m EVNTIDX using “C:\Program Files\ STATA\Stata2020\ASMVIS.dta”, keepusing(EVNTIDX) nogenerate keep(matc
> h)
(label eventype1 already defined)

Result # of obs.

not matched 0
matched 1,255

. save “C:\Program Files\ STATA\Stata2020\PMEDIDS.dta”, replace
file C:\Program Files\ STATA\Stata2020\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\Stata2020\a1.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\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\Stata2020\a1.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\a1.xlsx saved

. keep LINKIDX

. gen first=0

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

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

. drop first

. save “C:\Program Files\ STATA\Stata2020\PMEDIDS.dta”, replace
file C:\Program Files\ STATA\Stata2020\PMEDIDS.dta saved

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

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

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

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

. sort LINKIDX RXRECIDX

. *save “C:\Program Files\ STATA\Stata2020\PMED.dta”, replace

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

. merge m:m LINKIDX using “C:\Program Files\ STATA\Stata2020\PMEDIDS.dta”, nogenerate keep(match using)

Result # of obs.

not matched 0
matched 2,970

. save “C:\Program Files\ STATA\Stata2020\MVPMEDS.dta”, replace
file C:\Program Files\ STATA\Stata2020\MVPMEDS.dta saved

. sort LINKIDX RXRECIDX

. label variable RXRECIDX “rxrecidx”

. label variable RXNAME “rxname”

. label variable RXXP`yr'X “rxxp18x”

. label variable PERWT`yr'F “perwt18f”

. /* sample print of work.mvpmeds;
> PMED (HC-206A) 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\Stata2020\a1.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\a1.xlsx saved

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

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

. /* Total Rx expenditures associated with medical visits 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\Stata2020\a1.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\a1.xlsx saved

end of do-file
. exit, clear

MEPSdofileB.log


name: <unnamed>
log: C:\Program Files\ STATA\Stata2020\STATAdofileB.log
log type: text
opened on: 20 Jul 2020, 14:16:39

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

. local evntnum=206

. local condnum=207

. * Get condition records coded as asthma.
.

. use “C:\Program Files\ STATA\Stata2020\H`condnum'.dta”, clear

. keep if CCSR1X == “RSP009” & CCSR2X == “-1” & CCSR3X == “-1”
(92,815 observations deleted)

. keep CONDIDX CCSR1X CCSR2X CCSR3X 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 CCSR1X = 'RSP009' and CCSR2X = '-1' and CCSR3X = '-1'*/
. list if _n<=50, separator(0)

. export excel “C:\Program Files\ STATA\Stata2020\a2.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\a2.xlsx saved

. save “C:\Program Files\ STATA\Stata2020\ASCONDS.dta”, replace
file C:\Program Files\ STATA\Stata2020\ASCONDS.dta saved

. use “C:\Program Files\ STATA\Stata2020\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 7 “7 HVIS”, add

. label define eventype1 8 “8 PMED”, add

. label values EVENTYPE eventype1

. save “C:\Program Files\ STATA\Stata2020\CLNK.dta”, replace
file C:\Program Files\ STATA\Stata2020\CLNK.dta saved

. keep CONDIDX EVNTIDX EVENTYPE

. merge m:m CONDIDX using “C:\Program Files\ STATA\Stata2020\ASCONDS.dta”, keepusing(CONDIDX) nogenerate keep(mat
> ch)

Result # of obs.

not matched 0
matched 8,854

. save “C:\Program Files\ STATA\Stata2020\ASCLNKS.dta”, replace
file C:\Program Files\ STATA\Stata2020\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\Stata2020\a2.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\a2.xlsx saved

. sort EVNTIDX

. /* sample print of work.asclnks - sorted by evntidx */

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

. export excel “C:\Program Files\ STATA\Stata2020\a2.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\a2.xlsx saved

. keep EVNTIDX EVENTYPE

. gen first=0

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

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

. drop first

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

. export excel “C:\Program Files\ STATA\Stata2020\a2.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\a2.xlsx saved

. rename EVNTIDX LINKIDX

. save “C:\Program Files\ STATA\Stata2020\ASCLNKS.dta”, replace
file C:\Program Files\ STATA\Stata2020\ASCLNKS.dta saved

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

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

. sort LINKIDX RXRECIDX

. *save “C:\Program Files\ STATA\Stata2020\PMED.dta”, replace

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

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

Result # of obs.

not matched 0
matched 15,656

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

. save “C:\Program Files\ STATA\Stata2020\ASPMEDS.dta”, replace
file C:\Program Files\ STATA\Stata2020\ASPMEDS.dta saved

. sort LINKIDX RXRECIDX

. label variable RXRECIDX “rxrecidx”

. label variable RXNAME “rxname”

. label variable RXXP`yr'X “rxxp18x”

. label variable PERWT`yr'F “perwt18f”

. /* 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\Stata2020\a2.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\a2.xlsx saved

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

. export excel “C:\Program Files\ STATA\Stata2020\a2.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\a2.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\Stata2020\a2.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\a2.xlsx saved

end of do-file

. exit, clear

MEPSdofileC.log


name: <unnamed>
log: C:\Program Files\ STATA\Stata2020\STATAdofileC.log
log type: text
opened on: 17 Jul 2020, 17:31:03

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

. local evntnum=206

. local condnum=207

. * Get condition records coded as asthma.
.

. use “C:\Program Files\ STATA\Stata2020\H`condnum'.dta”, clear

. keep if CCSR1X == “RSP009” & CCSR2X == “-1” & CCSR3X == “-1”
(92,815 observations deleted)

. keep CONDIDX CCSR1X CCSR2X CCSR3X 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 CCSR1X = 'RSP009' and CCSR2X = '-1' and CCSR3X = '-1'*/

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

. export excel “C:\Program Files\ STATA\Stata2020\a3.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\a3.xlsx saved

. save “C:\Program Files\ STATA\Stata2020\ASCONDS.dta”, replace
file C:\Program Files\ STATA\Stata2020\ASCONDS.dta saved

. use “C:\Program Files\ STATA\Stata2020\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 7 “7 HVIS”, add

. label define eventype1 8 “8 PMED”, add

. label values EVENTYPE eventype1

. save “C:\Program Files\ STATA\Stata2020\CLNK.dta”, replace
file C:\Program Files\ STATA\Stata2020\CLNK.dta saved

. keep CONDIDX EVNTIDX EVENTYPE

. merge m:m CONDIDX using “C:\Program Files\ STATA\Stata2020\ASCONDS.dta”, keepusing(CONDIDX) nogenerate keep(mat
> ch)

Result # of obs.

not matched 0
matched 8,854

. save “C:\Program Files\ STATA\Stata2020\ASCLNKS.dta”, replace
file C:\Program Files\ STATA\Stata2020\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\Stata2020\a3.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\a3.xlsx saved

. sort EVNTIDX

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

. export excel “C:\Program Files\ STATA\Stata2020\a3.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\a3.xlsx saved

. keep EVNTIDX EVENTYPE

. gen first=0

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

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

. drop first

. save “C:\Program Files\ STATA\Stata2020\ASCLNKS.dta”, replace
file C:\Program Files\ STATA\Stata2020\ASCLNKS.dta saved

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

. export excel “C:\Program Files\ STATA\Stata2020\a3.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\a3.xlsx saved

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

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

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

. keep if PERWT`yr'F > 0
(2,787 observations deleted)

. save “C:\Program Files\ STATA\Stata2020\MVIS.dta”, replace
file C:\Program Files\ STATA\Stata2020\MVIS.dta saved

. sort EVNTIDX

. * Identify MVIS events which were for asthma.
.

. *keep EVNTIDX

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

Result # of obs.

not matched 0
matched 1,565

. save “C:\Program Files\ STATA\Stata2020\ASMVIS.dta”, replace
file C:\Program Files\ STATA\Stata2020\ASMVIS.dta saved

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

. export excel “C:\Program Files\ STATA\Stata2020\a3.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\a3.xlsx saved

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

. export excel “C:\Program Files\ STATA\Stata2020\a3.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\a3.xlsx saved

. /* Total medical visit expenditures 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\Stata2020\a3.xlsx”, replace firstrow(variables)
file C:\Program Files\ STATA\Stata2020\a3.xlsx saved

end of do-file


. exit, clear

Return 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