help csvconvert

Title

csvconvert - module for gathering multiple comma-separated values (.csv) files into one single Stata (.dta) dataset.

Syntax

csvconvert
input_directory
, replace [options]

Note: all .csv files must be placed in the same directory;
input_directory is the directory where the .csv files must be stored.

replace overwrites the existing output file (if it already exists).
This option is mandatory and must be specified in order to complete the conversion process.

output_file(file_name) file_name is the name of the .dta file; default is "output.dta".

output_dir(output_directory) output_directory is the directory where the output file is saved; default is input_directory.

input_file(.csv file list) names of the .csv files placed in input_directory to be converted;
you must specify the extension .csv for each file name included in the list (see examples below).
If this option is not specified, csvconvert takes into the process all the .csv files stored in input_directory.

Double quotes must NOT be used to enclose input_directory nor output_directory, even if the directory path contains spaces.

Description

csvconvert appends a set of .csv files into one single file,
which is saved in the .dta format, immediately readable into Stata.

This command suits the case in which the researcher holds multiple data files
differing by - for example - a period variable, typically year.

By default, csvconvert creates a new variable, _csvfile, containing the name of the .csv file from which the observation originates.

At the end of the process csvconvert displays a message with the number of the original .csv files that have been included in the .dta file.

(this information can become useful to double check that all the .csv files have been converted into the .dta file).

Once the process has been completed, type note to read the full list of .csv files included in the .dta file.

Examples

Download the trail sample from my webpage
and store it in a directory you created - for example C:\Data\worldbank.

Display the list of .csv files contained in the directory

. dir C:\Data\worldbank\*.csv

The following command creates the file output.dta and saves it in the directory C:\Data\worldbank

. csvconvert C:\Data\worldbank, replace

Display the full list of .csv files that have been converted and that are contained in output.dta

. note

The following command creates the file wb_data.dta and saves it in the directory C:\Data\wb dataset

. csvconvert C:\Data\worldbank, replace output_file(wb_data.dta) output_dir(C:\Data\wb dataset)

The following command includes in output.dta only the selected .csv files wb2008.csv and wb2009.csv

. csvconvert C:\Data\worldbank, replace input_file(wb2008.csv wb2009.csv)

Similar to the commands above: the files wb2008.csv and wb2009.csv are contained in wb_data.dta, which is saved in C:\Data\wb dataset

. csvconvert C:\Data\worldbank, replace input_file(wb2008.csv wb2009.csv) output_file(wb_data.dta) output_dir(C:\Data\wb dataset)

Reference

Gaggero A. (2014) csvconvert: A simple command to gather comma-separated value files into Stata, Stata Journal, Vol. 14(3), pp. 662-669.

Author

Alberto A. Gaggero, University of Pavia, Italy
alberto.gaggero@unipv.it

Also see

Manual: [D] append, [D] insheet

Help: [D] append, [D] insheet