Write dataset in memory to new do-file as an input command
writeinput varlist [if exp] [in range] using newfile.do [ , note(string) replace noclear ]
Description
writeinput writes data (or a subset of data) in memory to a do-file (.do) as an input statement. This is useful for creating self-contained data examples that can be distributed with code in a do-file. Since Stata 11, the do-file editor has larger file size capacity, so large example datasets can be included in do-files (see: http://www.stata.com/stata11/doedit.html).
When creating an example dataset or snippet to include in something like a homework example, lecture slide, email to Stata Technical Support, or a posting on Statalist, one might want to create an input (see input) command, allowing the recipient to directly write the example data into memory, rather than relying on copied/pasted free-form data (which might not format or wrap properly for the recipient and lead to miscommunication).
Additionally, input requires formats of string variables be included in the varlist as well as double quotes around string variable data where there is an embedded space. writepinput eases the process of creating a input version of the example data by adding this additional formatting automatically.
Required
using specifies newfile.do to be created and must have a .do file extension.
varlist is the list of variables to be included in the input command created in newfile.do
Options
replace option to replace existing do-file with the same name.
note(string) option to include a note (marked as a comment with asterisks) placed at the bottom of the input command written the the new do-file.
noclear specifies that no clear command is to be written to the top of the new do-file. This is useful for inputing data horizontally (as new variables) to a dataset already in memory (assuming the extant dataset has equal or more observations).
if and in are allowed.
Examples
. sysuse auto, clear . writeinput make mpg price for in 1/5 using "test1.do", repl . writeinput make mpg price for if for==0 in 20/60 using "test2.do", /// replace n(Here's some notes) . writeinput make if for==1 & pri>200 in 1/50 using "test3.do", /// replace n(write some notes here) . type "test3.do"
// input data from "test3.do" as new variables in the existing dataset // . rename make make2 . do "test3.do"
Author
Eric A. Booth, Texas A&M University ebooth@ppri.tamu.edu
Also see
On-line: help for input; file write; doedit