Title

File I/O -- Use, save, import, export data

Syntax

usel [path_el | <] [if] [in] [, options]

savel [path_el] [if] [in] [, options]

cd Changes the working directory to the directory of the data

keep(varelist) Uses or saves only the specified variables

pass(options) Passes import/export options through to other commands

Options for usel
sys Look for the file in the system directories; like sysuse

Options for savel
dropafter Drops the saved records from current data

makedir Creates the specified directory path, if it doesn't already exist.

label(text) Sets the dataset label

Description

usel and savel affect and are affected by the current data source, which is roughly the most recent external source of data, and is fully described under recent.

Common Features

o- They use path_el to specify the file, which is more flexible than a standard filepath.

o- There are no clear or replace options, existing files or data in memory will always be replaced.

o- File types other than Stata data files may be saved and used, by including their extensions.

o- References to non-existent variables in external files will not cause an error.

o- A file reference is optional when one can be determined from the current data source - which is usually the case.

Usel

Command Description

usel usel the current data source, if possible.

usel < Use the most recent valid dataset, prior to the current data source

usel path_el Use the specified file.

Note that usel < will search back before the current data source as far as necessary to find the most recent source it can use (ie, one that was set with usel or savel).

Savel

Command Description

savel Save using the directory and filename of the current data source, if possible.

savel path_el Save to the specified path.

savel path_el if/in/,keep() Save the specified subset of data.

if, in, and/or keep() can be used with savel as they are with most commands: Ie, they limit the observations and variables acted upon.
In particular, they determine the data that will be saved, but they do not affect the data in memory. They allow you to save a subset of the data.

When savel is specified with no main parameter, it will use the directory and filename of the current data source, and the .dta extension, if possible. In other words:

o- If the current data source is a .dta file, it will be overwritten.

o- If the current data source is a file with some other extension, a .dta version will be written, in the same directory, with the same name.

o- If the current data source is empty or remote, savel will require an explict path_el.

Options

General

cd changes the working directory to the directory containing the used or saved file.

keep(varlist) applies the command to the specified variables only.

pass() passes import/export options along to the appropriate handler.

o- For file extensions .txt or .csv, the options are those for import delimited or { help export delimited}.

o- For file extensions .xl, .xls or .xlsx, the options are described under portel xl.

o- For other file extensions (besides .dta), the options are those for callst.

Usel

sys specifies that the file to be useled is (correctly placed) in the system directories. When that is the case, sys is an alternative to specifying the entire path explicitly.

Savel

dropafter drops the records specified by if and in after they've been saved to another data file.

Note that while keep(varlist) identifies specific variables to be saved, this has no effect on dropafter; the entire records specified by if and in will be dropped.

makedir will create the directory path specified in path_el, if it doesn't already exist.

label(text) is just a shortcut for setting the dataset label.

version(#) specifies saving/exporting with an older file format: 11, 12, or 13 can be specified.

Examples

. usel a space filled name

. usel other-directory/goodies, cd

. usel blahblah/bl*/bl*/bl*/bl*

. usel <

. savel asubset in 1/100 if flag=="OK", keep(some-somemore another)