Jan 17

CRSP dates to good dates in Stata

Tag: stataadmin @ 11:07 am

Here is some simple code to transform those pesky CRSP dates (YYYYMMDD) to Stata compatible dates: insheet using "/Users/michaelewens/Archive/UCSD/returns_paper/data/stock_indices/sp_daily.csv"
rename caldt date
* Date Transformation gen year = floor(date/10000)
 gen month = floor((date - year*10000)/100)
 gen day = floor(date - year*10000 - 100*month)
 * replace the date
 replace date = mdy(month, day, year) 
format date %td


Oct 07

Stata Bundle for Textmate

Tag: mac, stataadmin @ 12:28 pm

Working with Stata’s do file editor is one of the most painful exercises. The lack of syntax highlighting means a large do file is basically unreadable. Enter TextMate. One can edit and send .do files to Stata directly from a Textmate project. If you have the standard Stata Intercooled, then download and install the Stata bundle. Next, go to “Bundles –> Bundle Editor –> Show Bundle Editor” and find the Stata bundle. Select the “Send File to Stata” command and edit the first line “StataMP” to read “Stata.” Do the same with the “Send Selection to Stata.” Now open Stata, cd to the project directory, open your do file in TextMate and start editing. Bask in the glory that is syntax-highlighting.


Sep 01

Convert strings to integers in Stata

Tag: stataadmin @ 6:47 pm

Suppose you have a string variable with a finite number of values (e.g. US states, industry, etc).  The encode command plus the generate command will create a new variable that assigns unique ids to each string value: 

encode your_string_variable, gen(new_string_as_int) 

The labels in the edit/browse view will look like strings, so make sure you use ‘nolabel’ on any export or tab commands. 


Jul 16

Plot multiple kernel densities on one graph in Stata

Tag: stataadmin @ 5:44 pm

If you want to compare kernel density estimates across years for a particular variable, putting each estimate on one graph will make it easy. The process is fairly straightforward in Stata (and even easier in Matlab…). First, we start with the simple ‘kdensity‘ command

kdensity income if year == 1990

Next, we append this command with the ‘addplot‘ function:

kdensity income if year == 1990, addplot(kdensity income if year == 1991)

and we can add even more with the ‘||’ syntax:

kdensity income if year == 1990, addplot(kdensity income if year == 1991 || kdensity income if year == 1992)

If we could use the ‘by’ option, this process would be much cleaner. Finally, we add a legend:

kdensity income if year == 1990, addplot(kdensity income if year == 1991 || kdensity income if year == 1992) legend(ring(0) pos(2) label(1 "1990") label(2 "1991") label(3 "1992"))


Jun 30

No undo in Stata do file editor

Tag: stataadmin @ 11:35 am

I persistently hit “command+S” or “cntrl+S” when editing any file. Unfortunately, in Stata’s built-in do file editor (on the Mac at least) once I hit such a command, the undo history clears. This is a horrible feature. All other modern editors keep a running history of all edits so that undo can be called as often as possible. Combine this problem with a lack of syntax-highlighting and no line-specific errors, and Stata is even more difficult to use.


Jun 24

Save Estimation Results in Stata

Tag: stata, tricksadmin @ 6:40 pm

JohnZ posted a solution to saving your coefficient estimates in Stata to a simple dataset. Here is the code:

*!
*! est2data.ado --
*!
*! Usage: est2data nbase outfname
*!
*! Saves the 'b' and 'se' vectors of the most recent estimation results
*! to a dataset named 'outfname', using 'nbase' as the base name for the
*! dataset columns.
*!
*! Also saves variable labels. Each row of the resulting dataset
*! is a coefficient estimate.
*!
program define est2data
preserve
args nbase outfname
drop in 1/`c(N)'
matrix bvec = e(b)'
matrix vvec = vecdiag(e(V))'
svmat bvec, names(`nbase'_est)
svmat vvec, names(`nbase'_se)
quietly: replace `nbase'_se = sqrt(`nbase'_se)

local vnames: rownames bvec
local numv: list sizeof vnames
quietly: gen `nbase'_names = ""
quietly: gen `nbase'_lbl = ""
forvalues ivar = 1/`numv' {
local thisv: word `ivar' of `vnames'
quietly: replace `nbase'_names = "`thisv'" in `ivar'
local thislbl "NA"
capture local thislbl: variable label `thisv'
quietly: replace `nbase'_lbl = "`thislbl'" in `ivar'
}
keep `nbase'_*
notes: "e(N) `e(N)'"
notes: "e(depvar) `e(depvar)'"
notes: "e(ll) `e(ll)'"
notes: "e(cmd) `e(cmd)'"
save `outfname', replace
end

Thanks John!


Jun 22

Textmate as the ultimate editor

Tag: latex, mac, os x, php, stataadmin @ 4:10 pm

With dozens of “bundles” that allow for application-specific editing, the Mac text editor TextMate is a great tool for the researcher. I posted four screenshots below for Stata, LaTeX, Matlab and PHP. The most powerful is PHP as there is a default bundle that lets you run the script directly from the file. With a little work (install TeX on your Mac), you can easily compile and view your LaTeX files. The quick command(e.g. “begin + “tab”) make editing a breeze. What I like most is the syntax highlighting that it brings to editing .m and .do files.  Moreover, you can create “projects” that let you easily handle multiple files.

Textmate with Stata

Textmate and Stata

Textmate with LaTeX

Textmate and Latex

Textmate with PHP

Textmate and PHP

Textmate with Matlab

Textmate and Matlab


Jun 19

Get list of unique variable values in Stata

Tag: stataadmin @ 5:12 pm

UPDATE:

A better solution to the problem posed below is:

levelsof country, local(levels)
foreach cnt of local levels {

More information available from the Stata FAQ.

Suppose you have a database of countries over time in the long format and you want a simple list of the countries. If that dataset is open, simply run the following:

preserve
by country, sort: gen nvals = _n
keep if nvals == 1
keep country
save country_list, replace
restore


Jun 11

Saved Results in Stata 10

Tag: stataadmin @ 9:58 pm

One of the more interesting feature in Stata 10 is the ability to save estimation results:

Stata 10 allows you to store estimation results in a file for use in later sessions. Fit a model today, save the results to disk, come back tomorrow, load the results, and continue with postestimation analysis as if you never exited Stata. Or email your results file to a colleague for further review. estimates save and estimates use make these tasks easy.

This new feature does not address a problem that continues to push me towards Matlab: the ability to save coefficient estimates, standard errors, r^2, etc in variables. For example, I am working on a modified version of the Heckman sample selection problem where the first stage is an ordered probit. I need to save some of the coefficient estimates for the second stage estimation. Right now, I have to cut and paste the results in the second stage .do file before running it. Annoying…


Jun 03

Format dates in Stata

Tag: stataadmin @ 1:10 pm

I tend to pass dates to Stata in the form “January 1, 2005″ because it gives me flexibility to get date information with the daily function. Transforming these dates into Stata’s daily date format type:

gen date = daily(original_date, "mdy");
format date %td

Now the date is of the form “01Jan2005.” Then, we can get years, days and months with the year, day and month functions. Stata’s full list of date functions is found here.

We can calculate the number of days, years or months between two dates using the reformatted dates and simple subtraction.  Stata gives each date an integer with 01Jan1960 =0.  So,

04Feb1960 – 05Jan1960 = 30

We can then divide this number by say 365 to get the number of years between two dates (controlling for the possibility of a negative value).