-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_unique.ado
More file actions
34 lines (32 loc) · 935 Bytes
/
count_unique.ado
File metadata and controls
34 lines (32 loc) · 935 Bytes
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
capture program drop count_unique
program count_unique, rclass
syntax varlist(min=1 max=1) [if] [in], [print(string)]
marksample i_use, strok
///////////////////////////////////////////////////////////////////////////////
// Description:
// ------------
// Counts unique values in a variable (including strings).
//
// Syntax:
// -------
// count_unique var [if] [in], [print(string)]
// MWE:
// ----
// sysuse auto
// count_unique make
// return list
//////////////////////////////////////////////////////////////////////////////
preserve
tempvar i_first
keep if `i_use'
bysort `varlist': gen byte `i_first' = (_n==1) & ~missing(`varlist')
if "`print'"!="" {
count if `i_first'
}
else {
qui count if `i_first'
}
restore
return scalar uval = r(N)
display as result r(N)
end