-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlcodebook.ado
executable file
·157 lines (141 loc) · 5.98 KB
/
lcodebook.ado
1
* lcodebook.adocapture program drop lcodebookprogram lcodebook syntax varlist using [if] [in] ,[ caption(string) label(string)] marksample touse, novarlist tempvar mysample mysample2 notvalidskip noskpsamp display _newline as text "Running " as result "\$Id: lcodebook.ado 1434 2010-02-23 13:54:10Z mesa $" generate `notvalidskip' = . generate `noskpsamp' = . generate `mysample' = . generate `mysample2' = . replace `mysample2' = 0 replace `mysample2' = 1 if x_year2 == 1 replace `mysample2' = `mysample2' & `touse' // latexclean_lc ,clean( "`caption'" ) // local mycaption "`r(x)'" // nospecials_lc ,clean( "`label'" ) // local mylabel "`r(x)'" #delimit ; capture file close myfile; file open myfile `using', write replace; /* this defines the header of the table */ file write myfile "\begin{longtable}{" _n; file write myfile ">{\hsize=.30\hsize}X" _n; file write myfile "|>{\hsize=.1\hsize}X" _n; file write myfile ">{\hsize=.15\hsize}Y" _n; file write myfile ">{\hsize=.25\hsize}X" _n; file write myfile ">{\hsize=.15\hsize}X" _n; file write myfile ">{\hsize=.15\hsize}X" _n; file write myfile ">{\hsize=.25\hsize}X" _n; file write myfile "|>{\hsize=.65\hsize}X" _n; file write myfile "}" _n; file write myfile "\caption{`caption'} \label{`label'} \\" _n; file write myfile "\hline Variable & Obs & Unique & Missing & Mean & Min & Max & Label \\ \hline " _n; file write myfile "\endfirsthead" _n; file write myfile "\multicolumn{8}{c}%" _n; file write myfile "{{\bfseries \tablename\ \thetable{}: `caption' -- continued}} \\" _n; file write myfile "\hline Variable & Obs & Unique & Missing & mean & Min & Max & Label \\ \hline" _n; file write myfile "\endhead" _n; file write myfile "\hline \multicolumn{8}{r}{{{\small(continued on next page)}}} \\ \hline" _n; file write myfile "\endfoot" _n; file write myfile "\hline" _n; file write myfile "\endlastfoot" _n; /* loop over all variables. Calculates several entries of the codebook table, saves them in a macro and writes the .tex-file line by line .... .*/ quietly describe,simple; /* Hier statt * eine Variablenliste? */ foreach y of var `varlist' { ; /* Handle Y2 variables */ // replace `mysample2' = 0; // replace `mysample2' = 1 if x_year2 > 1; // replace `mysample2' = `mysample2' & `touse'; replace `mysample' = `touse'; replace `mysample' = `mysample2' if substr("`y'",-3,.)=="_y2"; replace `notvalidskip' = 1; capture replace `notvalidskip' = 0 if `y'==.s; capture replace `notvalidskip' = 0 if `y'=="Valid Skip"; replace `noskpsamp' = ( `mysample' & `notvalidskip' ); /* take care of LaTeX special in Variablenames */ local x = subinstr("`y'","_","\_",.); local latexlabelname = subinstr("`y'","_","",.); /* calculate entries of table */ quietly count if `mysample'; local observ = r(N); quietly unique `y' if `mysample'; /* local observ = r(N); */ local unique = r(sum); quietly tabmiss `y' if `noskpsamp'; local missings = r(sum); local missingsperc = r(mean)*100; /* local missingsperc = round(r(missingsperc),0.1); */ local missingsperc = string(`missingsperc',"%9.0f"); /* Deal with time formats. */ local myformat : format `y'; if substr("`myformat'",1,2) != "%t" { ; local myformat "%9.1f"; } ; quietly sum `y' if `mysample'; local mean= round(r(mean),0.1); // local mean = string(`mean',"%9.1f"); local mean = string(`mean',"`myformat'"); local min = r(min); // local min = string(`min',"%9.1f"); local min = string(`min',"`myformat'"); local max = round(r(max),0.1); // local max = string(`max',"%9.1f"); local max = string(`max',"`myformat'"); local labelofvariable: variable label `y'; /* My addition -- take care of LaTeX special characters in the Variable Labels. */ local labelofvariable = subinstr("`labelofvariable'","_","\_",.); local labelofvariable = subinstr("`labelofvariable'","#","\#",.); local labelofvariable = subinstr("`labelofvariable'","$","\$",.); local labelofvariable = subinstr("`labelofvariable'","%","\%",.); local labelofvariable = subinstr("`labelofvariable'","&","\&",.); local labelofvariable = subinstr("`labelofvariable'","^","\^",.); local labelofvariable = subinstr("`labelofvariable'","{","\{",.); local labelofvariable = subinstr("`labelofvariable'","}","\}",.); /* file write myfile " \texttt{`x'} & `observ' & `unique' & `missings' (`missingsperc' \%) &`mean' & `min' & `max' & `labelofvariable' \medskip \\ " _n; */ file write myfile " \hyperref[`latexlabelname']{\texttt{`x'}} & `observ' & `unique' & `missings' (`missingsperc'\%) &`mean' & `min' & `max' & `labelofvariable' \medskip \\ " _n; } ; /* ende der tex-tabelle schreiben */ file write myfile "\hline" _n; file write myfile "\end{longtable}" _n; file close myfile; #delimit crend# delimit ;capture program drop latexclean_lc;program latexclean_lc, rclass; syntax ,clean(string); local the_string = subinstr("`clean'","_","\_",.); local the_string = subinstr("`the_string'","#","\#",.); local the_string = subinstr("`the_string'","$","\$",.); local the_string = subinstr("`the_string'","%","\%",.); local the_string = subinstr("`the_string'","&","\&",.); local the_string = subinstr("`the_string'","^","\^",.); local the_string = subinstr("`the_string'","{","\{",.); local the_string = subinstr("`the_string'","}","\}",.); local the_string = subinstr("`the_string'",`"""'," ",.); return local x "`the_string'";end;capture program drop nospecials_lc;program nospecials_lc, rclass; syntax ,clean(string); local the_string = subinstr("`clean'","_","",.); local the_string = subinstr("`the_string'","#","",.); local the_string = subinstr("`the_string'","$","",.); local the_string = subinstr("`the_string'","%","",.); local the_string = subinstr("`the_string'","&","",.); local the_string = subinstr("`the_string'","^","",.); local the_string = subinstr("`the_string'","{","",.); local the_string = subinstr("`the_string'","}","",.); return local x "`the_string'";end;