The SAS max() function allows you to find the maximum value across columns and returns the biggest number in a data step.
data example;
a = max(1,4,7);
put a;
b = max(1,.,-1);
put b;
run;
/* Log Output */
7
1
When working with different data in programming, the ability to be able to easily find summary statistics of pieces of your data is valuable.
One such case is if you want to find the maximum number of multiple values.
In a SAS data step, you can find the maximum number of multiple numbers or multiple columns with the max() function.
Below shows you some simple examples of using max() in a SAS data step.
data example;
a = max(1,4,7);
put a;
b = max(1,.,-1);
put b;
run;
/* Log Output */
7
1
Find Maximum Across Columns in Data Step with max()
You can use max() to create a new column which has the maximum value across a number of columns in a SAS dataset.
For example, let’s say you had some variables and wanted to find the maximum of each observation for those variables.
To do this, you can use max() and pass the column names as the arguments.
Below shows you how to create a new column which is the maximum value across the columns A, B and C in a data step.
data example;
input A B C;
datalines;
5 1 2
4 2 3
3 3 4
2 4 5
1 5 6
;
run;
data example_with_max;
set example;
M = max(A,B,C);
run;
/* Output */
A B C M
5 1 2 5
4 2 3 4
3 3 4 4
2 4 5 5
1 5 6 6
Treatment of Missing Values with SAS max() Function in Data Steps
If your data has missing values, then you have to be aware of how max() calculates the maximum if you have values which are missing that are passed to max().
In general, missing values will be ignored by max().
This is shown below, where I’ve included one missing value in the call to max().
data example;
a = max(1,.,-1);
put a;
run;
/* Log Output */
1
However, if all values are missing, then max() will return a missing value.
data example;
a = max(.,.,.);
put a;
run;
/* Log Output */
.
Find Maximum of Entire Column in SAS with PROC MEANS
If you want to find the maximum of an entire column, then you should use PROC MEANS.
In SAS, PROC MEANS is a procedure which allows you to create summaries of your data and allows you to calculate things like the sum, mean, max, max, etc. of a variable.
You can find the maximum of an entire column by specifying the MIN option with PROC MEANS.
Below shows you how to find the maximum of an entire column using the SAS PROC MEANS procedure.
data example;
input group $ value;
datalines;
A 1
A 2
A 3
B 4
B 5
B 6
C 7
C 8
;
run;
proc means data=example max;
variable value;
run;
Find Minimum Across Columns in Data Step with max() Function
If you’d instead like to find the minimum of values in a SAS data step, you can use the SAS min() function.
min() works in the same way as max() and allows you to find the minimum across multiple columns in a SAS data set.
Below shows some simple examples of how you can use min() in a SAS data step.
data example;
a = min(1,4,7);
put a;
b = min(1,.,-1);
put b;
run;
/* Log Output */
1
-1
Hopefully this article has been useful for you to learn how to use the SAS max() function in your SAS programs.