I would like to create a custom table that describes a dataset and includes the variable name, definition (label), number of observations, and the first and last year the variable shows up in the panel. I have written code to do this using a putdocx, but cannot figure out how to get the same results using the table and collect commands for export to LaTeX. Here is the working code:
// Add a report title and cover information
use AmazonData, clear
local reportdate `"`c(current_date)'"'
local reporttime `"`c(current_time)'"'
putdocx clear
putdocx begin, pagenum(decimal) footer(footer1)
putdocx paragraph, tofooter(footer1) halign(center)
putdocx pagenumber
putdocx paragraph
putdocx text ("Brazilian Amazon Data Collected to Date"), bold linebreak font("Calibri", 12)
putdocx text ("Date: `reportdate' `reporttime'"), font("Calibri", 9) linebreak linebreak
* Add the variable names below.
local variables codigo year forestMB def_annMB
* the following lines of code create a summary stats table with varible name, definition, number of observations, first year and last year
local nvars: word count `variables'
local nrows=`nvars'+2 /* the number of rows in the table is the number of variables + 2: one for the header and one for the table title */
putdocx table table2 = (`nrows',5), border(all, nil) width(100%) layout(autofitcontents)
putdocx table table2(1,1)=("Summary Statistics: Municipality Level Data for the Amazon"), bold font("Calibri", 12) halign(center) colspan(7) linebreak
putdocx table table2(2,1) = ("Variable"), border(bottom)
putdocx table table2(2,2) = ("Variable Definition"), border(bottom)
putdocx table table2(2,3) = ("Obs."), border(bottom)
putdocx table table2(2,4) = ("Beginning year"), border(bottom)
putdocx table table2(2,5) = ("End year"), border(bottom)
local i=3
foreach var of varlist `variables' {
local lab: variable label `var'
gen ymin`var' =year if `var'!=.
gen ymax`var' =year if `var'!=.
quietly sum ymin`var' ymax`var'
local ymin`var'=`r(min)'
local ymax`var'=`r(max)'
quietly summarize `var'
putdocx table table2(`i',1) = ("`var'")
putdocx table table2(`i',2) = ("`lab'")
putdocx table table2(`i',3) = (r(N))
putdocx table table2(`i',4) = (`ymin`var'')
putdocx table table2(`i',5) = (`ymax`var'')
local i=`i'+1
}
drop ymin* ymax*
putdocx save "$tables\WB_DataSummary", replace
exit
And the resulting table (which should describe the data well enough to understand this question):
This is my attempt using the table command:
use AmazonData, clear
* Add the variable names below.
local variables codigo year forestMB def_annMB
// calculate the min and max year for each variable
foreach var of varlist `variables' {
local lab: variable label `var'
gen ymin`var' =year if `var'!=.
gen ymax`var' =year if `var'!=.
quietly sum ymin`var' ymax`var'
collect get r(min) r(max)
local ymin`var'=`r(min)'
local ymax`var'=`r(max)'
}
local var codigo year
table (var), style(table-1) stat (n `var')
collect layout
collect style row stack, nospacer
collect export WB_table.tex, replace tableonly
Any help on how I can add these scalars when using a table command is welcome. Thanks!
// Add a report title and cover information
use AmazonData, clear
local reportdate `"`c(current_date)'"'
local reporttime `"`c(current_time)'"'
putdocx clear
putdocx begin, pagenum(decimal) footer(footer1)
putdocx paragraph, tofooter(footer1) halign(center)
putdocx pagenumber
putdocx paragraph
putdocx text ("Brazilian Amazon Data Collected to Date"), bold linebreak font("Calibri", 12)
putdocx text ("Date: `reportdate' `reporttime'"), font("Calibri", 9) linebreak linebreak
* Add the variable names below.
local variables codigo year forestMB def_annMB
* the following lines of code create a summary stats table with varible name, definition, number of observations, first year and last year
local nvars: word count `variables'
local nrows=`nvars'+2 /* the number of rows in the table is the number of variables + 2: one for the header and one for the table title */
putdocx table table2 = (`nrows',5), border(all, nil) width(100%) layout(autofitcontents)
putdocx table table2(1,1)=("Summary Statistics: Municipality Level Data for the Amazon"), bold font("Calibri", 12) halign(center) colspan(7) linebreak
putdocx table table2(2,1) = ("Variable"), border(bottom)
putdocx table table2(2,2) = ("Variable Definition"), border(bottom)
putdocx table table2(2,3) = ("Obs."), border(bottom)
putdocx table table2(2,4) = ("Beginning year"), border(bottom)
putdocx table table2(2,5) = ("End year"), border(bottom)
local i=3
foreach var of varlist `variables' {
local lab: variable label `var'
gen ymin`var' =year if `var'!=.
gen ymax`var' =year if `var'!=.
quietly sum ymin`var' ymax`var'
local ymin`var'=`r(min)'
local ymax`var'=`r(max)'
quietly summarize `var'
putdocx table table2(`i',1) = ("`var'")
putdocx table table2(`i',2) = ("`lab'")
putdocx table table2(`i',3) = (r(N))
putdocx table table2(`i',4) = (`ymin`var'')
putdocx table table2(`i',5) = (`ymax`var'')
local i=`i'+1
}
drop ymin* ymax*
putdocx save "$tables\WB_DataSummary", replace
exit
And the resulting table (which should describe the data well enough to understand this question):
Summary Statistics: Municipality Level Data for the Amazon |
||||
Variable | Variable Definition | Obs. | Beginning year | End year |
codigo | unique 7-digit municipality code | 15660 | 2000 | 2019 |
year | year | 15660 | 2000 | 2019 |
forestMB | forest cover MapBiomas , km2 | 14857 | 2000 | 2018 |
def_annMB | deforestation MapBiomas, km2 | 14415 | 2000 | 2018 |
use AmazonData, clear
* Add the variable names below.
local variables codigo year forestMB def_annMB
// calculate the min and max year for each variable
foreach var of varlist `variables' {
local lab: variable label `var'
gen ymin`var' =year if `var'!=.
gen ymax`var' =year if `var'!=.
quietly sum ymin`var' ymax`var'
collect get r(min) r(max)
local ymin`var'=`r(min)'
local ymax`var'=`r(max)'
}
local var codigo year
table (var), style(table-1) stat (n `var')
collect layout
collect style row stack, nospacer
collect export WB_table.tex, replace tableonly
Any help on how I can add these scalars when using a table command is welcome. Thanks!