tabgen.ado converts categorical variables, with value labels, into binary of 0 and 1. It converts the value labels into variable labels. The advantage of using this function is that it can be used to make do file more reproducible with few errors. Copy and save it into your ado folder in Stata working directory.
/*
Created by: Waiguru Muriuki
Email: waigurusamuel@gmail.com
Date : 16/03/2021
Affiliation: Evidence Action
*/
cap program drop tabgen
program define tabgen,
pause on
version 15.0
syntax varlist (max=1) [,mi ///
missing ///
range(string) ///
VALuelabel(string)]
local var `varlist'
if "`range'" == "" {
local range 1(1)20
}
if "`other'" == "" {
local other -996
}
if "`missing'" == "" {
local missing -998 -999
}
qui labellist `var'
if "`valuelabel'" == "" {
local valuelabel "`r(lblname)'"
}
***************************************************************************************************************
***Checking whether the primary variable is string or numeric if string exit
qui destring `var',replace
if substr("`:type `var''" , 1, 3)== "str" {
di as err "Only works with numeric variables, preferably with value labels"
exit 198
}
****************************************************************************************************
***Confirming whether the variable, used for this program, exist in the data set.
forval lo=1(1)20 {
foreach varo in `var'_`lo' `var'_`lo' {
capture confirm variable `varo', exact
if _rc==0 {
di in red "Drop and regenerate the `var'_? variables"
exit 198
}
}
}
*************************************************************************************************
forval x=`range'{
qui gen `var'_`x' = .
qui replace `var'_`x' = 0 if `var'!=.
qui replace `var'_`x' = 1 if inlist( `var',`x')
if "`valuelabel'" != "" {
local varname "`:label `valuelabel' `x''"
label var `var'_`x' "`varname'"
}
}
**************************************************************************************************************************
foreach x in `other' {
local abs_x = subinstr("`x'", "-", "", 1)
qui gen `var'_`abs_x' = .
qui replace `var'_`abs_x' = 0 if `var'!=.
qui replace `var'_`abs_x' = 1 if inlist( `var',`other')
if "`valuelabel'" != "" {
local varname "`:label `valuelabel' `x''"
label var `var'_`abs_x' "`varname'"
}
}
*********************************************************************************************************
foreach xx in `missing' {
local abs_x = subinstr("`xx'", "-", "", 1)
qui gen `var'_`abs_x' = .
qui replace `var'_`abs_x' = 0 if `var'!=.
qui replace `var'_`abs_x' = 1 if inlist( `var',`xx')
if "`valuelabel'" != "" {
local varname "`:label `valuelabel' `xx''"
label var `var'_`abs_x' "`varname'"
}
}
***********************************************************************************************************
foreach vv of varlist `var'_* {
qui sum `vv'
if r(mean) == 0 & r(max) == 0 & r(min) == 0 {
drop `vv'
di in green "`vv' dropped because all values were zero or missing"
}
}
end