Gregory A. Huber

Forst Family Professor of Political Science, Department of Political Science, Yale University

Contact and other information is available on my homepage.

(Computer) Code

Computers have vastly simplified the collection and analysis of large amounts of data. Doing this efficiently, however, requires being able to leverage the power of the software and hardware now available to us. These code snippets, mostly Stata and Excel for the time being, are tools I've developed along the way in my own work which may be of use to you. Good luck. As always, comments and corrections are appreciated.

Additionally, the replication archives post on my research page include raw code that I regularly re-use rather than developing solutions from scratch.

This is a set of Stata code snippets that I've found to be useful for automating certain research tasks.

Capture variable labels in local variable:

local templabel : var label (variablename)

This puts the label assigned to the variable variablename in the local variable templabel. I've found that when this code crashes, omitting the parentheses will sometimes make it work:

local templabel : var label variablename.

Capture variable value labels in local variable:

This code is useful for generating graphs labeled with the value of a variable for which the graph was constructed.

local templabel : label (variablename) `valuetostore'

This puts the label assigned to the variable variablename for the value valuetostore in the local variable templabel

Loop outreg over multiple model specifications, appending results after the first specification:

foreach var of varlist A B C { regress `var' BLAH if "`var'"=="A" { outreg using outreg.out, replace } else { outreg using outreg.out, append } }

This runs the same regression with each dependent variable (A, B, and C) and stores the output in the file outreg.out

Reformat Outreg Output in Excel:

This Excel macro reformats Stata outreg opened in Excel into easily readable tables. It works with output created by the versions of outreg published in sg97.3 and before. To avoid security concerns, the code of the macro is available in plain text here. (It has been reported to me that this does not work with some more recent versions of Excel, nor on a Mac.) A sample outreg command is:

outreg using outreg.out, se bracket rdec(3) 3aster ctitle("Title") addnote("Note") replace