*! version 1.1 09apr2018

Title

multimport - Import and append multiple excel or delimited files

Syntax

multimport
import_method
, [directory(dir:filespec) extension(multimport##extension:ext1 [ext2 ...]) include(filename:file1 [file2 ...]) options]

import_method is the method to use for reading non-Stata data into memory (see [D] import); currently delimited and excel are supported

filespec is any valid Mac, Unix, or Windows path (see [D] dir)

directory(dir:filespec) directory of files to import; if not specified, it defaults to the current working directory

extensions(multimport##extension:ext1 [ext2 ...]) override file extension(s) scanned in directory(filespec); if not specified, sensible defaults are inferred from import_method

include(filename:file1 [file2 ...]) specific filenames to import

exclude(filename:file1 [file2 ...]) specific filenames to exclude from import

importoptions(import:import_method_opts) pass options to [D] import import_method

appendoptions(append:append_opts) pass options to [D] append

force skip user confirmation prompt

clear replace data in memory

Description

multimport is a simple tool for importing multiple non-Stata data into memory, appending them automatically.
According to the specified import_method, it works similarly to import delimited or import excel.
However, it automatically scans the directory specified in directory(filespec), looking for all files with extensions that are inferred from import_method:

- multimport excel looks for *.xls and *.xlsx files.

- multimport delimited looks for *.csv files.

These defaults may be overridden with the extensions(ext1 [ext2 ...]) option.

multimport will list all files that will be imported and ask the user for confirmation before continuing, unless the force option is specified to override this prompt.
The program will import and append all listed files, generating a new variable _filename indicating the source filename.
Additional optiones may be passed to [D] import import_method and [D] append using importoptions(import_method_opts) and appendoptions(append_opts), respectively.

Specific files in directory(filespec) may be excluded from the import with the exclude(file1 [file2 ...]) option.
Alternatively, a specific list of files to be imported may be indicated with the include(file1 [file2 ...]) option.

Options

directory(dir:filespec) directory where files to be imported are located. If not specified, it will default to the current working directory.

extensions(multimport##extension:ext1 [ext2 ...]) overrides the default file extensions of files that multimport will import.
By default, multimport scans the directory specified in directory(filespec), looking for all files with extensions that are inferred from import_method:

multimport excel looks for *.xls and *.xlsx files.

multimport delimited looks for *.csv files.

These default extensions can be modified with the extension(multimport##extension:ext1 [ext2 ...]) option. For example, in order to import delimited files that are stored both as *.csv or *.txt files with

. multimport delimited, extensions(csv txt)

include(filename:file1 [file2 ...]) allows to indicate specific files to be imported, instead of importing all files in directory(dir:filespec). For example,

. multimport excel, include("svy2011.xls" "svy2012.xls" "svy2013.xls")

exclude(filename:file1 [file2 ...]) allows to indicate specific files to be excluded from the automatic import.

importoptions(import:import_method_opts) allows to pass options to import import_method. For example, it can be used to specify a specific separation character for delimited files:

. multimport excel, importoptions(delimiter(";"))

appendoptions(append:append_opts) allows to pass options to append.
For example, it can be used to keep specified variables from appending dataset(s):

. multimport excel, appendptions(keep(id strata sex race age))

Examples - delimited files

Setup - Use NHANES II data and export delimited files by region number

. use http://www.stata-press.com/data/r14/nhanes2.dta

. levelsof region

. foreach r in `r(levels)' {

. export delimited using nhanes_reg`r' if region == `r'

. }

Simple use - Import all delimited files in current working directory

. multimport delimited, clear

As above, this time explicitly specifying the current directory and skipping confirmation prompt.

. multimport delimited, clear directory(.) force

Import only specific filenames.

. multimport delimited, clear include(nhanes_reg1.csv nhanes_reg4.csv)

Examples - Excel files

Setup - Use NHANES II data and export Excel (*.xls) files by region number

. use http://www.stata-press.com/data/r14/nhanes2.dta

. levelsof region

. foreach r in `r(levels)' {

. export excel using nhanes_reg`r' if region == `r'

. }

Import all Excel files except one

. multimport excel, clear exclude(nhanes_reg1.xls)

Import all Excel data as strings

. multimport excel, clear importoptions(allstring)

Stored results

multimport stores the following in r():

Macros
r(directory) directory as typed

r(files) list of imported filenames

Author

Alvaro Carril

National Bureau of Economic Research

Email: alvaroc@nber.org