Category Archives: Stata

grid paper using Stata

I just wrote a program to make grids using Stata! Not much use for many people, but if you have a friend who is a knitter and needs a grid template, well this may come in handy 🙂

This is an ado file, so just: 1) copy the text below the image into a text editor; 2) save it with the extension “.ado”; 3) put it in your Stata personal directory

The following grid was generated using the command:
mkgrid 60 80 "44 162 95" vthin label alt
grid_60x80


/**
Program to produce a y by x grid
Carl Higgs 16 April 2016

Input parameters (arguments):
y number of vertical spaces
x number of horizontal spaces
colour line colour
width line width
labels whether numbered axes are desired (if so, write "label" here, or leave blank)
alt if you have specified having labels, you might want them to alternate: type "alt"

e.g.
mkgrid 60 80
mkgrid 60 80 black
mkgrid 60 80 black vthin label
mkgrid 60 80 black vthin label alt
**/
* capture program drop mkgrid
program mkgrid, rclass
version 13.1
args y x colour width labels alt
di "y is `y' and x is `x'"
drop _all
loc obs = max(`y',`x')
set obs `obs'
if `y'==`obs' {
egen y = fill(1(1)`y')
gen x = `x'
}
else {
gen y = `y'
egen x = fill(1(1)`x')
}

loc aspect = `y'/`x'

if "`labels'"=="label" {
tw sc y x, ms(i) ///
graphregion(fcolor(white) lcolor(white)) scheme(s2color) ///
xlab(0(1)`x',grid nogextend gmin glc("`colour'") `alt' labsize(vsmall) glwidth("`width'")) ///
ylab(0(1)`y',grid nogextend gmin glc("`colour'") `alt' angle(h) labsize(vsmall) glwidth("`width'")) ///
ytitle("") ///
xtitle("") ///
aspect(`aspect') ///
xsca(noextend) ysca(noextend) ///
name(grid_`y'x`x',replace)
}

else {
tw sc y x, ms(i) ///
graphregion(fcolor(white) lcolor(white)) scheme(s2color) ///
xlab(0(1)`x',grid nogextend gmin glc("`colour'") glwidth("`width'")) ///
ylab(0(1)`y',grid nogextend gmin glc("`colour'") glwidth("`width'")) ///
ytitle("") ///
xtitle("") ///
aspect(`aspect') ///
xsca(off) ysca(off) ///
name(grid_`y'x`x',replace)
}

end

A 5×5 grid:
grid_5x5

A 2496×738 grid, towards the limits of what this code can do, the artifacts of electrical glitch spirits start to surface:
grid_2496x738

Keyboard shortcuts for Stata/IC 13

Here is a consolidated list of keyboard shortcut commands for Stata, as well as some directions on how to form custom macro strings. I have formed this list from various Stata help files as well as my own trial and error:

Tab: completes a variable’s name in the command line sequence, as far as ambiguity allows (if there are similarly named variables)
Esc: clears the command sequence

Move cursor to various panes:
Ctrl+Tab: reverse cycles through the various panes
Ctrl+Shift+Tab: forward cycles through the various panes
Ctrl + 1: command pane
Ctrl + 2: output pane
Ctrl + 3: review log
Ctrl + 4: variable pane (also: pressing space bar whilst a variable is selected in the variable pane inserts this variable into the command line sequence)
Ctrl + 5: properties pane
Ctrl + 7: viewer window
Ctrl + 8: data window

Function keys:

F1: enters the command ‘help advice’
F2: enters the command ‘describe’
F3: opens up a ‘find’ tab in the output pane to search for specific text
F7: inserts the command ‘save’ into the command sequence
F8: inserts the command ‘use’ into the command sequence

Alt+F4: quits the program (I think this maybe a general windows command)

Customise function keys
Entering a command sequence of global F[number]commands assigns a function key to a command sequence such as “list;” (leave out the square brackets), with the semi-colon indicating entry of a command. This is a special form of global macro – see below.

Custom macros
Entering a command sequence of global [macro][commands] saves a sequence of words which you can later recall by prefix-ing the macro with $, that is $[macro]. You can get quite fancy with this it seems, so if interested read more in the pdf file “Programming Stata”: http://www.stata.com/manuals13/u18.pdf

________________

Coming from a background of using design software where shortcut usage is common, I find it surprising a command heavy program such as Stata doesn’t make keyboard shortcut information more readily accessible. Ideally, shortcut commands would be displayed when hovering a cursor over a menu command or displayed next to the command. Entry of these commands via shortcuts would still be recorded in the command log, so from a workflow perspective I can’t understand why this hasn’t been implemented in a mature program which many people use.