Archives for the ‘Stata’ Category

The limits of encapsulation

I just read this. I liked it. It put some bit of anguish I've been having into clearer words than I could. My Stata code between 2000 and 2006 consisted exclusively of do-files that put to work either standard Stata commands or user-written commands from the SSC. There was not a single program definition anywhere [...]

Using Mata for string processing

My friend Dan Blanchette showed me a little Mata function yesterday that he wrote for changing the case -- lower, upper, proper -- for strings longer than 244 characters. It was fresh in my head today as I went looking for something while babysitting my daughter -- can't remember what; babysitting requires undivided attention -- [...]

Filling the gaps in your panel with winsor

I recently worked on a project where I had to model groundwater salinity as an indirect function of population growth. The idea is that more people will draw more fresh water from the aquifer; other things equal, saline water will be displace it. I had to do this for sixteen counties in Southern Florida and [...]

Automated sanity checks

I am reading An Introduction to Stata Programming, by Christopher Baum. He suggests, in Chapter 5.2, a nice do-file method to validate your data: you use pairs of list and assert. For example, suppose you know that a variable v should have no missing values. If it indeed does not, then assert !missing(v) should run [...]

Count missing observations

With one variable, that's easy enough: count if missing(variable-name). If you have several variables, you can put them in a foreach loop. But if you have to do this for arbitrary lists of variables in several files, it may be interesting to package that foreach loop inside a quick command that might handle special display [...]

Making Vim run Stata and clean up after itself

Last week I mentioned that in the course of switching from Notepad++ to Vim I lost the ability to run Stata do-files or selected lines from within the text editor, and I asked my readers for help if they had a solution. What do you know, one of them did, and wrote to me all [...]

Program vs. include smackdown

When it comes to defining local macros in a different place from where you use them, you have two options: a do-file you include as needed or an r-class program that you call as needed. I talked about it here and said that a program is a better choice, without any evidence to back up [...]

I switched to Vim

I was looking for an excuse to try something new and I decided to pick on one Notepad++ shortcoming that was handy: the Stata syntax highlighting gets utterly mangled after compound quotes -- `"`like so'"' -- which do sometimes arise, usually in the process of file open/file write.  Vim does not get confused by compound quotes [...]

Calling irregular arguments with syntax anything

The other day I wrote a program that needed to call a file as an argument -- with the full file path. My first pass at it was to capture the argument as usual, with say args input_file. But that would not have worked with file paths that have spaces in them. What might have [...]

Define local macros in one place, use them everywhere

In The Stata Journal Vol. 9, No. 3, 2009 there's a Stata tip (# 77) on re-using macros in multiple do-files, by Jeph Herrin. His solution is to define any local macros in a separate do-file, say locals.do. You can call that do-file with the include command at the top of any do-file that might [...]