forked from theDubW/printcase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
printcase.ado
276 lines (239 loc) · 6.8 KB
/
printcase.ado
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//MADE BY MAX WEINREB
program printcase
version 17.0
syntax anything(everything id = "if id_variable == id_val"), [pdf font(string) noempty ignore(string asis) replace ADDNotes width(string) unit(string) LANDscape noheader nofooter]
tokenize `anything', parse("==")
local fileName = ""
local varName = ""
local varNum = ""
//checking for correct syntax and assigning macros
if("`2'" != "=="){
display "Must have '==' expression"
error 198
}
if("`3'" == ""){
display "Cannot have empty id_val"
error 198
}
local varNum = "`3'"
tokenize `1'
if("`1'" != "using" & "`1'" != "if"){
display "Must follow printcase syntax"
error 198
}
else if("`1'" == "if"){
local varName `2'
}
else {
local fileName = "`2'"
if("`3'" != "if"){
display "Must have if expression"
error 198
}
local varName `4'
}
//checking for extra unnecessary arguments
if("`fileName'" != ""){
if("`5'" != ""){
display "Too many arguments passed"
error 198
}
}
else{
if("`3'" != ""){
display "Too many arguments passed"
error 198
}
}
//checking the types of varName and varNum. Must be of same type (either numeric or string)
local varType = "num"
capture confirm numeric variable `varName'
if(_rc != 0){
local varType = "str"
}
//Setting up documents, either pdf or word
display "ID Variable: `varName'"
display "ID Value: `varNum'"
if("`font'" == "") {
local docFont = "Arial"
}
else {
local docFont = "`font'"
}
local doccmd putdocx
if "`pdf'" != ""{
local doccmd putpdf
}
local indexSlash = strrpos("`c(filename)'", "\")
local myFile = substr("`c(filename)'", `indexSlash'+1, strlen("`c(filename)'")-`indexSlash')
`doccmd' clear
local landsc = ""
if("`landscape'" != ""){
local landsc = "landscape"
}
//Title and setting up document
if("`pdf'"!=""){
putpdf begin, font("`docFont'") `landsc'
//Title
putpdf paragraph
putpdf text (`"`varName' `varNum'"'), font("`docFont'",30) bold
//Headers
if("`header'" == ""){
putpdf paragraph
putpdf text ("User: `c(username)'"), font("`docFont'",20)
putpdf paragraph
putpdf text (`"`myFile'"'), font("`docFont'",20)
putpdf paragraph
putpdf text ("Date Printed: `c(current_date)'"), font("`docFont'",20)
}
}
else{
putdocx begin, pagenum(decimal) footer(footer1) font("`docFont'") `landsc'
//Title
putdocx paragraph, style(Title)
putdocx text (`"`varName' `varNum'"'), font("`docFont'")
//Headers
if("`header'" == ""){
putdocx paragraph, style(Heading1)
putdocx text ("User: `c(username)'"), font("`docFont'")
putdocx paragraph, style(Heading1)
putdocx text (`"`myFile'"'), font("`docFont'")
putdocx paragraph, style(Heading1)
putdocx text ("Date Printed: `c(current_date)'"), font("`docFont'")
}
//Pagenumbers + footer1
if("`footer'" == ""){
putdocx paragraph, tofooter(footer1)
putdocx pagenumber
putdocx text (" printcase_`varNum'_`myFile'")
}
}
`doccmd' paragraph
//Retreiving list of variables
quietly describe, varlist
local dataVars `r(varlist)'
local rowNum = `r(k)'
local ++rowNum
//if longitudinal find # columns, initiliaze longitudinal variable list
local colNum = 3
local numWaves = 1
tempvar id_wave
preserve
//find # waves
if("`varType'" == "num"){
quietly count if `varName' == `varNum'
}
else {
quietly count if `varName' == "`varNum'"
}
local numWaves = `r(N)'
sort `varName', stable
if("`varType'" == "num"){
quietly by `varName': generate `id_wave' = _n if `varName' == `varNum'
}
else {
quietly by `varName': generate `id_wave' = _n if `varName' == "`varNum'"
}
local colNum = 2+`numWaves'
//Initializing table
if("`width'" != ""){
`doccmd' table tbl = (`rowNum', `colNum'), width(`width')
}
else {
`doccmd' table tbl = (`rowNum', `colNum')
}
`doccmd' table tbl(1,1) = ("Variable Name")
`doccmd' table tbl(1,2) = ("Variable Label")
local colTitle = "Response"
if("`unit'" != ""){
local colTitle = "`unit'"
}
forvalues w = 1/`numWaves'{
local col = 2+`w'
`doccmd' table tbl(1,`col') = ("`colTitle' "+"`w'")
}
local i = 2
foreach var in `dataVars' {
//Prevent rows from overflowing to next page
`doccmd' table tbl(`i', .), nosplit
//Response for case
local skipRow = 0
local varlabel : value label `var'
//used to keep track of skip conditions in longitudinal cases
local numSkips = 0
//iterate through all waves
forvalues wave = 1/`numWaves'{
quietly levelsof `var' if `id_wave' == `wave', clean
local toPrintValue = "`r(levels)'"
//if variable has no labelbook
if(`"`varlabel'"' != ""){
//account for missing values in labelbook
if("`r(levels)'"==""){
quietly levelsof `var' if `id_wave' == `wave', missing
}
local toPrintValue : label `varlabel' `r(levels)', strict
//get rid of special character that cannot be outputted
local toPrintValue : subinstr local toPrintValue "`=char(96)'" "`=uchar(8219)'", all
if "`r(levels)'" != "" & "`toPrintValue'"=="" {
local toPrintValue = "`r(levels)'"
}
}
//drop unwanted responses
if "`empty'"!="" & (regexm("`toPrintValue'", "(^\.[a-z]?$)|(^\s*$)")){
local numSkips = `numSkips' + 1
}
else if (`"`ignore'"' != "") {
foreach skip of local ignore {
if("`toPrintValue'" == "`skip'"){
local numSkips = `numSkips' + 1
continue, break
}
}
}
//print value
local col = `wave'+2
`doccmd' table tbl(`i', `col') = ("`toPrintValue'")
}
if(`numSkips' == `numWaves'){
`doccmd' table tbl(`i', .), drop
local i = `i' - 1
}
else {
// variable name
local toPrintVar = `"`var'"'
local toPrintVar : subinstr local toPrintVar "`=char(96)'" "`=uchar(8219)'", all
`doccmd' table tbl(`i', 1) = (`"`toPrintVar'"')
//variable label
local label : variable label `var'
local toPrintLabel = `"`label'"'
local toPrintLabel : subinstr local toPrintLabel "`=char(96)'" "`=uchar(8219)'", all
if("`addnotes'"!=""){
local toPrintNote : char `var'[note1]
if("`toPrintNote'"!=""){
local toPrintNote = "N: `toPrintNote'"
`doccmd' table tbl(`i', 2) = ("`toPrintLabel'"), linebreak
`doccmd' table tbl(`i', 2) = ("`toPrintNote'"), append font("`docFont'", 9)
}
else{
`doccmd' table tbl(`i', 2) = ("`toPrintLabel'")
}
}
else{
`doccmd' table tbl(`i', 2) = ("`toPrintLabel'")
}
}
local i = `i'+1
}
//filename and location options
local temp_replace = ""
if("`replace'"!=""){
local temp_replace = "replace"
}
if("`fileName'"!=""){
`doccmd' save "`fileName'", `temp_replace'
}
else{
//clean varNum to remove any characters that cannot be in a valid filename
`doccmd' save "`varName'`varNum'", `temp_replace'
}
end