# -----------------------------------------------------------------------------             
# R programming statements for h246 data                                              
#                                                                                           
# This file contains programming statements needed to import the ASCII data                 
# file (.dat) into R. The R programming language has the capability to produce              
# appropriate standard errors for estimates from a survey with a complex sample             
# design such as the Medical Expenditure Panel Survey (MEPS).                               
#                                                       
# The input file is the ASCII data file (h246.dat) supplied in this PUF                
# release, which can be extracted from the .zip file supplied at the MEPS                   
# website: https://meps.ahrq.gov/mepsweb/data_stats/download_data_files.jsp                 
#                                                       
# This code imports the MEPS data into R as a data frame called 'h246'.            
#                                                       
# Note that additional packages are needed to successfully run this code. To                
# install these packages, run the 'install.packages' function (shown below).                
# Once installed, the packages can be called using the 'library' function.                  
# Packages only need to be installed once, but they must be called using the                
# 'library' function every time a new R session is started.                                 
#                                                       
# Two options are available to run this code:                                               
#                                                       
#  1. Copy and paste the code into an interactive R session.                                
#                                                       
#     The user must first download the ASCII (.dat) file from the MEPS website              
#     and save it to a local directory, which must be defined in the                        
#     'meps_path' variable below. In this example, the local directory is                   
#     called 'C:/MEPS'. Note that the path structure will differ on Mac and PC.             
#                                                       
#                                                       
#  2. Call this code directly from an interactive R session.                                
#                                                       
#  (a) If the ASCII (.dat) file has already been downloaded from the MEPS                   
#         website and saved to a local directory, the following code can be run             
#         (after re-defining the 'meps_path' variable to point to the location              
#         of the h246.dat file.)                                   
#                                                       
#     meps_path <- "C:/MEPS/h246.dat"                             
#     source("https://meps.ahrq.gov/mepsweb/data_stats/download_data/pufs/h246/h246ru.txt")
#     head(h246) # view data                                       
#                                                       
#  (b) Alternatively, the ASCII (.dat) file can be downloaded directly from                 
#         the MEPS website. The following code can be used to download and                  
#         import the h246 data into R without having to manually download,             
#         unzip, and store the file on your local computer.                         
#                                                       
#           url <- "https://meps.ahrq.gov/mepsweb/data_files/pufs/h246dat.zip" 
#           download.file(url, temp <- tempfile())                              
#                                                       
#     meps_path <- unzip(temp, exdir = tempdir())                               
#     source("https://meps.ahrq.gov/mepsweb/data_stats/download_data/pufs/h246/h246ru.txt")
#                                                       
#           unlink(temp)  # Unlink to delete temporary file                         
#                                                       
#           head(h246) # view data                                 
#                                                       
# -----------------------------------------------------------------------------             
                                                        
# DEFINE 'meps_path' -----------------------------------------------------------            
# 'meps_path' should point to the file path of the ASCII file (h246.dat)               
# Here, the 'exists' function checks whether meps_path is already defined. This             
# feature is useful if calling this file from an external source.                           
if(!exists("meps_path"))  meps_path = "C:/MEPS/h246.dat"      

# INSTALL PACKAGES ------------------------------------------------------------
# Uncomment and run this portion if packages are not yet installed         
#                                          
# install.packages("readr")                            
                                           
# **************************************
# LOAD PACKAGES ---------------------------------------------------------------

# Run this for every new R session      

library(readr)                          

# DATA FILE INFO ------------------------------------------

# Define start and end positions to read fixed-width file  


pos_start <- c(
1, 15, 28, 31, 42, 52, 59, 62, 63, 64, 
66, 68, 72, 74, 78, 79, 80, 82, 84, 86, 
88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 
108, 112, 117, 119, 121, 126, 128, 130, 132, 134, 
136, 138, 140, 143, 145, 147, 149, 151, 153, 155, 
157, 164, 166, 168, 170, 172, 174, 176, 178, 180, 
182, 184, 186, 191, 196, 198, 200, 207, 209, 218, 
220, 226, 228, 230, 232, 241, 243, 245, 247, 249, 
251, 253, 261, 263, 272, 274, 283, 285, 287, 289, 
291)


pos_end <- c(
14, 27, 30, 41, 51, 58, 61, 62, 63, 65, 
67, 71, 73, 77, 78, 79, 81, 83, 85, 87, 
89, 91, 93, 95, 97, 99, 101, 103, 105, 107, 
111, 116, 118, 120, 125, 127, 129, 131, 133, 135, 
137, 139, 142, 144, 146, 148, 150, 152, 154, 156, 
163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 
183, 185, 190, 195, 197, 199, 206, 208, 217, 219, 
225, 227, 229, 231, 240, 242, 244, 246, 248, 250, 
252, 260, 262, 271, 273, 282, 284, 286, 288, 290, 
292)


var_names <- c(
"JOBSIDX", "JOBIDX", "JOBNUM", "ESTBIDX", "DUPERSID", "DUID", "PID", "RN", "ORIGRND", "PANEL", 
"JSTRTM", "JSTRTY", "JSTOPM", "JSTOPY", "RETIRJOB", "SUBTYPE", "STILLAT", "TYPECHGD", "MAIN_JOB", "DIFFWAGE", 
"STILLWORKFTPT", "WHYCHNGPTTOFT", "WHYCHNGFTTOPT", "STILLWRK", "OFFTAKEI", "NOWTAKEI_M22", "ESTBTHRU", "ESTBTHRU_M24", "INSESTB", "INSESTB_M24", 
"HIDISAVW", "RVWTOTNUMEMP", "WHY_LEFT_M18", "JOBTYPE", "NUMEMPS", "ESTMATE1_M19", "MORELOC", "BUSINC", "PROPRIET", "TYPEEMPL", 
"YLEFT_M18", "YNOBUSN_M18", "HRSPRWK", "HRS35WK", "TEMPJOB", "SESNLJOB", "SICKPAY", "PAYDRVST", "PAYVACTN", "RETIRPLN", 
"WKLYAMT", "EMPLINS", "JOBHASHI", "OFFRDINS", "OFFRDINS_M24", "DIFFPLNS", "DIFFPLNS_M24", "ANYINS", "ANYINS_M24", "INUNION", 
"EMPLUNIONPROV", "HHMEMBER_M18", "TOTLEMP_M18", "TOTNUMEMP", "SALARIED", "HOWPAID", "DAYWAGE", "HRSPRDY", "MAKEAMT", "PERUNIT_M18", 
"HRLYWAGE", "MORE10", "MORE15", "MOREMINM", "GROSSPAY", "GROSSPER", "SALRYWKS", "HRSALBAS", "EARNTIPS", "EARNBONS", 
"EARNCOMM", "TIPSAMT", "TIPSUNIT_M18", "BONSAMT", "BONSUNIT", "COMMAMT", "COMMUNIT", "INDCODEX", "INDCAT17", "OCCCODEX", 
"OCCCAT18")


var_types <- c(
"c", "c", "c", "c", "c", "c", "n", "n", "n", "n", 
"n", "n", "n", "n", "n", "n", "n", "n", "n", "n", 
"n", "n", "n", "n", "n", "n", "n", "n", "n", "n", 
"c", "n", "n", "n", "n", "n", "n", "n", "n", "n", 
"n", "n", "n", "n", "n", "n", "n", "n", "n", "n", 
"n", "n", "n", "n", "n", "n", "n", "n", "n", "n", 
"n", "n", "n", "n", "n", "n", "n", "n", "n", "n", 
"n", "n", "n", "n", "n", "n", "n", "n", "n", "n", 
"n", "n", "n", "n", "n", "n", "n", "n", "n", "n", 
"n")


var_types <- setNames(var_types, var_names)

# IMPORT ASCII file -----------------------

h246 <- read_fwf(                      
meps_path,                                 
     col_positions =                       
         fwf_positions(                    
             start = pos_start,            
                 end   = pos_end,          
                 col_names = var_names),   
         col_types = var_types)            

                                           
# OPTIONAL: save as .Rdata file for easier loading ----------------------------
# Run this to save a permanent .Rdata file in the local working directory      
#                                          
# save(h246, file ="h246.Rdata")  
                                           
# -----------------------------------------------------------------------------
# NOTES:                                       
#                                          
#  1. This program has been tested on R version 4.4.0              
#                                          
#  2. This program will create a temporary data frame in R called 'h246'.      
#     You must run the 'save' command to permanently save the data to a local  
#     folder                                   
# -----------------------------------------------------------------------------