-
Notifications
You must be signed in to change notification settings - Fork 90
/
util_get_var_min_max.sas
69 lines (56 loc) · 2.33 KB
/
util_get_var_min_max.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
/***
Create a global symbol that contains the MIN and MAX of numeric values, space-delimited
-INPUT
DS data set containing the numeric variable for which you want MIN and MAX
REQUIRED
Syntax: (libname.)memname
Example: ANA.ADVS
VAR variable on DS containing non-missing values
REQUIRED
Syntax: variable-name
Example: AVAL
SYM name of symbol (macro variable) to declare globally and assign the result
REQUIRED
Syntax: symbol-name
Example: aval_min_max
EXTRA Additional values to include in resulting range. E.g., to ensure axis range include Normal Range interval.
optional
Syntax: Space-delimited list of numeric values
Example: -5 0 10
SQLWHR complete SQL where expression, to limit check to subset of DS data
optional
Syntax: where sql-where-expression
Example: where studyid = 'STUDY01'
-OUTPUT
&SYM, a global symbol containing the MIN and MAX, space-delimited
Author: Dante Di Tommaso
***/
%macro util_get_var_min_max(ds, var, sym, sqlwhr=, extra=);
%global &sym;
%local OK minval maxval idx nxt;
%let OK = %assert_dset_exist(&ds);
%if &OK %then %let OK = %assert_var_exist(&ds, &var);
%if &OK %then %do;
proc sql noprint;
select min(&var), max(&var) into :minval, :maxval
from &ds
&sqlwhr;
quit;
%let minval = &minval;
%let maxval = &maxval;
%*--- Adjust range to accomodate any extra values provided ---*;
%if %length(&extra) > 0 %then %do idx = 1 %to %sysfunc(countw(&extra, %str( )));
%let nxt = %scan(&extra, &idx, %str( ));
%if %sysevalf(&minval > &nxt) %then %let minval = &nxt;
%if %sysevalf(&maxval < &nxt) %then %let maxval = &nxt;
%end;
%let &sym = &minval &maxval;
%if &minval = . or &maxval = . %then
%put WARNING: (UTIL_GET_VAR_MIN_MAX) Missing vals for %upcase(&var) on %upcase(&ds). %upcase(&sym) = &&&sym...;
%else
%put NOTE: (UTIL_GET_VAR_MIN_MAX) Successfully created symbol %upcase(&sym) = &&&sym...;
%end;
%else %do;
%put ERROR: (UTIL_GET_VAR_MIN_MAX) Unable to read values from variable %upcase(&var) on data set %upcase(&ds).;
%end;
%mend util_get_var_min_max;