-
Login or Register
- Log in with
hep putdocx paragraph
clear all
cd "c:\temp"
sysuse auto, clear
putdocx begin, footer(footer_pn) pagesize(A4)
//footer
putdocx paragraph, tofooter(footer_pn)
putdocx pagenumber
putdocx text ("/")
putdocx pagenumber, totalpages
//title
putdocx paragraph, style(Heading1)
mata: st_local("fn",pathbasename(`"`c(filename)'"'))
local fl : data label
putdocx text ("Codebook for `fn'")
if "`fl'" != "" {
putdocx text (": `fl'"), italic
}
//data properties
putdocx paragraph, style(Heading2)
putdocx text ("Properties of file")
putdocx table file = (3,2), layout(autofitcontents)
putdocx table file(1,1) = ("no. of variables"), bold
putdocx table file(1,2) = ("`c(k)'")
putdocx table file(2,1) = ("no. of observations"), bold
putdocx table file(2,2) = ("`=_N'"), nformat(%9.0gc) trim
putdocx table file(3,1) = ("last saved"), bold
putdocx table file(3,2) = ("`c(filedate)'")
putdocx table file(.,.), border(all,nil)
putdocx table file(1,.), border(top,single)
putdocx table file(3,.), border(bottom, single)
//list of variables
putdocx paragraph, style(Heading2)
putdocx text ("List of variables")
putdocx table vars = (`=`c(k)'+1',2), layout(autofitcontents)
putdocx table vars(1,1) = ("variable name"), bold
putdocx table vars(1,2) = ("label"), bold
local i = 2
foreach var of varlist * {
putdocx table vars(`i' , 1) = ("`var'") ,
putdocx table vars(`i++', 2) = (`"`: variable label `var''"'),
}
putdocx table vars(.,.), border(all, nil)
putdocx table vars(1,.) , border(bottom, single) border(top, single)
putdocx table vars(`=`c(k)'+1',.) , border(bottom, single)
//properties of variables
foreach var of varlist * {
// title
putdocx pagebreak
putdocx paragraph, style(Heading2)
putdocx text ("`var'")
if `"`: variable label `var''"' != "" {
putdocx text (`": `: variable label `var''"'), italic
}
// collect info and frequency table or summary statistics
capture confirm string variable `var'
local tab = _rc == 0
local rawtype = cond(`tab', "string", "numeric")
local type : type `var'
tempvar mark
bys `var' : gen byte `mark' = _n == 1 if !missing(`var')
count if `mark' == 1
local tab = (r(N) <= 10) | `tab'
local n_distinct = r(N)
// variable properties
putdocx paragraph, style(Heading3)
putdocx text ("Properties of variable")
putdocx table desc_`var' = (4,2), layout(autofitcontents)
putdocx table desc_`var'(1,1) = ("type"), bold
putdocx table desc_`var'(1,2) = ("`rawtype' (`type')")
putdocx table desc_`var'(2,1) = ("missing values"), bold
count if missing(`var')
putdocx table desc_`var'(2,2) = (r(N)), nformat(%9.0gc) trim
putdocx table desc_`var'(3,1) = ("non-missing values"), bold
putdocx table desc_`var'(3,2) = (_N-r(N)), nformat(%9.0gc) trim
putdocx table desc_`var'(4,1) = ("distinct non-missing values"), bold
putdocx table desc_`var'(4,2) = (`n_distinct'), nformat(%9.0gc) trim
putdocx table desc_`var'(.,.), border(all, nil)
putdocx table desc_`var'(1,.), border(top, single)
putdocx table desc_`var'(4,.), border(bottom, single)
if `tab' {
putdocx paragraph , style(Heading3)
putdocx text ("Table")
frame
local data = r(currentframe)
frame copy `data' table, replace
frame change table
tempvar freq
bysort `var' : gen `freq' = _N
by `var' : keep if _n == 1
local val_lab : value label `var'
if "`val_lab'" != "" {
tempvar lab
decode `var', gen(`lab')
local label "label"
}
keep `lab' `var' `freq'
order `var' `lab' `freq'
rename `var' value
if "`val_lab'" != "" {
rename `lab' label
label values value .
}
rename `freq' frequency
putdocx table tab_`var' = data(value `label' frequency), varnames layout(autofitcontents)
putdocx table tab_`var'(.,.), border(all, nil)
putdocx table tab_`var'(1,.), border(top, single) bold
putdocx table tab_`var'(1,.), border(bottom, single)
putdocx describe tab_`var'
putdocx table tab_`var'(`r(nrows)',.), border(bottom, single)
frame change `data'
}
else {
putdocx paragraph , style(Heading3)
putdocx text ("Summary")
sum `var', detail
putdocx table tab_`var' = (5,2), layout(autofitcontents)
putdocx table tab_`var'(1,1) = ("minimum"), bold
putdocx table tab_`var'(2,1) = ("25th percentile"), bold
putdocx table tab_`var'(3,1) = ("50th percentile"), bold
putdocx table tab_`var'(4,1) = ("75th percentile"), bold
putdocx table tab_`var'(5,1) = ("maximum"), bold
putdocx table tab_`var'(1,2) = (r(min))
putdocx table tab_`var'(2,2) = (r(p25))
putdocx table tab_`var'(3,2) = (r(p50))
putdocx table tab_`var'(4,2) = (r(p75))
putdocx table tab_`var'(5,2) = (r(max))
putdocx table tab_`var'(.,.), border(all, nil)
putdocx table tab_`var'(1,.), border(top, single)
putdocx table tab_`var'(5,.), border(bottom, single)
}
}
//close
putdocx save cb, replace
hep putdocx paragraph
hep putdocx paragraph
input byte(a b) 1 1 1 2 end input byte(a c) 1 3 1 4 1 5 end
. list, clean
a b c _merge
1. 1 1 3 Matched (3)
2. 1 2 4 Matched (3)
3. 1 2 5 Matched (3)
. list a b c _merge, clean
a b c _merge
1. 1 1 3 both in master and using data
2. 1 1 4 both in master and using data
3. 1 1 5 both in master and using data
4. 1 2 3 both in master and using data
5. 1 2 4 both in master and using data
6. 1 2 5 both in master and using data
tempname a b input byte(a b) 1 4 1 6 2 9 3 3 5 . end sort a save `a', replace list drop _all input byte(a c) 1 2 2 8 2 3 3 5 3 6 4 . end sort a save `b', replace list use `a', clear merge m:m a using `b' sort a b c list use `a', clear joinby a using `b', unmatched(both) sort a b c list a b c _merge
select coalesce(a.a, b.a) as a,
a.b, b.c
from one as a full join two as b
on a.a=b.a
order by a,b,c;
Leave a comment: