Skip to main content

-writeinput- available from SSC

-writeinput- was recently posted to the SSC Archive[1][2].  I've written a bit about it before.  My announcement of this program focuses on using this program for creating a self-contained dataset example or snippet that can help other Statalist posters understand your question or response, but I've found the same is true for transmitting do-file examples to coworkers and students. It's very much in the spirit of Statalist FAQ.
Many times a simple data example would prevent a lot of confusion, but creating one isn't always convenient.  Sometimes users can post relevant data examples using one of Stata's canned datasets or by building fake data through a series of -generate- commands.  Some Statalist posters will simply try to explain their data, which sometimes causes confusion and varying interpretations of the problem and the data structure. Other users might copy and paste a snippet of the data, but wrapping can be a problem. Plus, others have to add double quotes to string variable values with embedded spaces in order to get the data into Stata to respond to the thread.
If you really want to get your point across, writing an -input- example is a good option.  However, this can be time consuming.  You'll want to put quotes around string variable values, and formats for variables you define.  -writeinput- helps automate this process by letting you create a -input- statement for the entire dataset or a selected subset of your data.
Finally, since the do-file editor in Stata 11 was improved to accommodate very large files, you can now save example datasets (or entire datasets -- though I wouldn't suggest it) along with do-file code.  -writeinput- makes all of this easier.
Here's an example:
clear
//install writeinput from SSC//
cap which writeinput
if _rc ssc install writeinput, replace
//example//
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"



Update:
It occurs to me that, for really wide data examples, wrapping still might be an issue when pasting the -input- command generated by -writeinput- into an email.  In this case, -input- behaves the same as other commands that allow the "///" comment indicator to be used for wrapping commands (see -help comments-).  So, in a previous post about -obsdiff- I used the example:
clear
inp    var1 str9 var2  var3  var4 str9(var5 var6)  var7 str9 var8   var9 str9(var10 var11)
1 "a" 1 2 "c" "s" 3 "d" 5 "AA" "z"
1 "a" 1 2 "c" "s" 3 "d" 5 "BB" "z"
1 "a" 1 2 "c" "s" 3 "d" 5 "CC" "z"
2 "a" 1 2 "c" "s" 3 "d" 5 "CC" "z"
end 

which is equivalent to the example:
clear
inp   var1 str9 var2  ///
var3  var4 str9(var5 var6) ///
var7 str9 var8   var9 ///
str9(var10 var11) 
1 "a" 1 2 "c" "s" ///
3 "d" 5 "AA" "z" 
1 "a" 1 2 "c" "s" ///
3 "d" 5 "BB" "z" 
1 "a" 1 2 "c" "s" ///
3 "d" 5 "CC" "z" 
2 "a" 1 2 "c" "s" ///
3 "d" 5 "CC" "z" 
end 
except that the chances for wrapping issues in the second example are much lower. Granted, it's not as easy to view the table without actually loading it into Stata.
 In the next update of -writeinput-, I'll try to include an option to set the line size or wrapping for the input command created by -writeinput- so that the above command can be created.

Comments