-
Notifications
You must be signed in to change notification settings - Fork 8
/
dstcnt.sas
75 lines (62 loc) · 2.4 KB
/
dstcnt.sas
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
%macro DstCnt(ds,dstvar,outvar,where=) / des="Obtain distinct count of a variable";
/********************************************************************************
BEGIN MACRO HEADER
********************************************************************************
Name: DstCnt
Author: Chris Swenson
Created: 2009-11-25
Purpose: Obtain distinct count of a variable
Arguments: ds - input data set
dstvar - variable to count distinct values
outvar - output macro variable containing count
where= - filter to apply to input data set
Revisions
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Date Author Comments
¯¯¯¯¯¯¯¯¯¯ ¯¯¯¯¯¯ ¯¯¯¯¯¯¯¯
YYYY-MM-DD III Please use this format and insert new entries above
********************************************************************************
END MACRO HEADER
********************************************************************************/
/* Check arguments */
%if "&ds"="" %then %do;
%put %str(E)RROR: No data set specified.;
%return;
%end;
%if %eval(%sysfunc(exist(&ds, %str(DATA))) + %sysfunc(exist(&ds, %str(VIEW))))=0 %then %do;
%put %str(E)RROR: Data set or view does not exist.;
%return;
%end;
%if "&dstvar"="" %then %do;
%put %str(E)RROR: No distinct variable specified.;
%return;
%end;
/* Set default for output variable */
%if "&outvar"="" %then %let outvar=outvar;
%else %do;
%global &outvar;
%end;
/* Modify where criteria */
%local addwhere;
%if %superq(where)=%str() %then %do;
%put NOTE: No where criteria specified.;
%let addwhere=;
%end;
%else %let addwhere= and %unquote(%superq(where));
/* Obtain distinct count of specified variable */
%local intovar;
proc sql noprint;
select count(distinct &dstvar)
into :intovar
from &ds
where not missing(&dstvar) &addwhere
;
quit;
%let &outvar=&intovar;
/* Output count */
data _null_;
format count comma20.;
count=&intovar;
put "NOTE: Distinct count of &dstvar in &ds: " count;
run;
%mend DstCnt;