-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexampleobs.ado
More file actions
87 lines (80 loc) · 2.31 KB
/
Copy pathexampleobs.ado
File metadata and controls
87 lines (80 loc) · 2.31 KB
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
* PROGRAM TO DISPLAY EXAMPLE OBSERVATIONS
*! v1.0.1 skh 11oct2015
* v1.0.0 skh date unknown
* CHANGES
* 10-11-2015 Made faster using random number generator instead of preserve and sample
capture program drop exampleobs
program define exampleobs
version 10
syntax [varlist] [if] [in] [, n(integer 10) NOHeader Header Label LOcal(string)]
if `n'<1 {
di as error in scml "{bf n} must be greater than or equal to 1"
exit
}
if wordcount("`varlist'")==1 & "`header'"=="" {
local noheader noheader
}
if wordcount("`varlist'")>1 & "`local'"!="" {
di as error in smcl "{varlist} must contain only one variable if {bf local()} is specified"
exit
}
if strpos("`local'"," ") {
di as error in smcl "{bf local()} must not contain spaces"
exit
}
if "`label'"!="" local nolabel ""
else local nolabel nolabel
if "`varlist'"=="" local varlist _all
if "`if'`in'"!="" {
preserve
qui keep `if' `in'
}
if _N==0 {
exit
}
else if _N<`n' {
list `varlist', clean noobs `nolabel' `noheader'
exit
}
else {
// old way (slow)
* preserve
* if wordcount("`if' `in'")>0 qui keep `if' `in'
* qui sample `n', count
* list `varlist', clean noobs `nolabel' `noheader'
// a faster way without the memory requirements of preserve (unless `if' `in', then preserve still needed)
local c=0
local randoms ""
forval i=1/`n' {
local ++c
if `c'>1 {
local comma ","
}
else local comma ""
cap set_rc // not a real command; sets _rc>0
while _rc {
local random = round(1+(_N-1)*runiform(),1)
if "`randoms'"!="" cap assert !inlist(`random',`randoms')
else cap assert 1==1 // to set _rc==0
}
local randoms `randoms'`comma'`random'
}
list `varlist' if inlist(_n,`randoms'), clean noobs `nolabel' `noheader'
// save as a local
if "`local'"!="" {
local values ""
local randoms_no_comma = subinstr("`randoms'",","," ",.)
local c=0
foreach random of local randoms_no_comma {
local ++c
if `c'>1 local space " "
else local space ""
local values `"`values'`space'`: di `varlist'[`random']'"'
qui di " // for syntax highlighting in Notepad++
}
// undocumented way to create a local in ado file (found in levelsof.ado):
c_local `local' `"`values'"'
qui di "
}
}
end