Skip to content

Instantly share code, notes, and snippets.

@pbaylis
Last active September 14, 2015 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pbaylis/7c641b91fcea1e32d3d8 to your computer and use it in GitHub Desktop.
Save pbaylis/7c641b91fcea1e32d3d8 to your computer and use it in GitHub Desktop.
Stata program to generate dummy variable bins of a variable. Stata program to generate dummy variable bins of a variable. Bottom and top bins run from edge to negative infinity and infinity, respectively. Also generates good-looking custom labels for regression tables.
program define makebins, rclass
syntax varname, [PREfix(name) min(real 0) max(real 100) step(real 5) SEQuential DROPbin(real 70)]
local var `varlist'
if mod(`max' - `min',`step') != 0 {
di as error "Warning: Step size `step' does not divide evenly into range. Top bin may extend beyond `max'."
}
* Declare prefix
if "`prefix'" != "" {
local p `prefix'
}
else {
local p b_`var'_
}
capture drop `p'* // Drop everything with this prefix
local numbins = ceil((`max' - `min') / `step') + 2
local maxbin = `numbins' - 1
tempvar binvar
qui gen int `binvar' = floor((`var' - `min') / `step') + 1
qui replace `binvar' = 0 if `binvar' < 0
qui replace `binvar' = `maxbin' if `binvar' > `maxbin'
* Create labeled bin dummies
forvalues b = 0/`=`numbins'-1' {
* Labels for all bins
local bmin = `b' * `step' + `min' - `step'
local bmax = `b' * `step' + `min'
* Change labels for edge bins
local bminl "`bmin'"
local bmaxl "`bmax'"
* Create dummy, label it
if ("`sequential'" != "") { // The user requested sequential varnames
local nametemp "`p'`b'"
}
else { // Default is nonsequential, include limits in varnames
// Locals to store the variable name, rounded
local bmin_n = round(`bmin',0.01)
local bmax_n = round(`bmax',0.01)
// Replace negatives with "M", periods with "P"
local bmin_n: subinstr local bmin_n "-" "M"
local bmin_n: subinstr local bmin_n "." "P"
local bmax_n: subinstr local bmax_n "-" "M"
local bmax_n: subinstr local bmax_n "." "P"
// Set -Inf and Inf as outer limits for edge bins
if (`b' == 0) {
local bmin_n "MInf"
}
if (`b' == `maxbin') {
local bmax_n "Inf"
}
local bmin_l "`bmin_n'"
local bmax_l "`bmax_n'"
local nametemp "`p'`bmin_n'_`bmax_n'"
}
if (`b' == 0) {
local bmin_l "-\infty"
}
if (`b' == `maxbin') {
local bmax_l "\infty"
}
// Generate, unless this is the bin we want to drop
if "`dropbin'" == "" || !inrange(`dropbin',`bmin', `bmax'-0.0001) {
quietly gen `nametemp' = `binvar' == `b'
label var `nametemp' "[`bmin_l', `bmax_l')"
}
else {
local dropbin_n "`nametemp'"
}
}
// Return what would have been the variable name of the dropped bin in r(dropbin_n)
return local dropbin_n "`dropbin_n'"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment