Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

SAS operator


May 26, 2021 SAS


Table of contents


Operators in SAS are symbols used in mathematical, logical, or comparison expressions. These symbols are built into the SAS language, and many operators can be combined in a single expression to give the final output.

The following is a list of SAS operator categories.

  • Arithmetic operator
  • The logical operator
  • Comparison operator
  • Min/Max operator
  • The connection operator

Let's look at it one by one. Operators are always used with variables that are part of the data analyzed by the SAS program.

Arithmetic operator

The following table describes the details of the arithmetic operator. Let's assume that the two data variables V1 and V2 have values of 8 and 4, respectively.

The operator Describe Cases
+ Plus V1 + V2 = 12
- Subtraction V1-V2 = 4
* Multiplication V1 V2 * 32 =
/ Except V1 / V2 = 2
** Power V1 V2 ** = 4096

Cases

DATA MYDATA1;
input @1 COL1 4.2	@7 COL2 3.1; 
Add_result = COL1+COL2;
Sub_result = COL1-COL2;
Mult_result = COL1*COL2;
Div_result = COL1/COL2;
Expo_result = COL1**COL2;
datalines;
11.21 5.3
3.11  11
;
PROC PRINT DATA=MYDATA1;
RUN;

After running the above code, we can get the output below.

SAS operator

The logical operator

The following table describes the details of the logical operator. T hese operators evaluate the True value of the expression. Therefore, the result of the logical operator is always 1 or 0. Let's assume that the two data variables V1 and V2 have values 8 and 4, respectively.

The operator Describe Cases
And operator. If both data values are true, the result is 1, otherwise 0. (V1 and 2 V2 and 3) gives 0.
| The OR operator. If any one of the data values is true, the result is 1, otherwise it is 0. (V1 and 9 V2; 3) is 1.
Not operator. The NOT operator results in an expression with a value of FALSE or a missing value of 1, otherwise 0. NOT (V1; 3) is 1.

Cases

DATA MYDATA1;
input @1 COL1 5.2	@7 COL2 4.1; 
and_=(COL1 > 10 & COL2 > 5 );
or_ = (COL1 > 12 | COL2 > 15 );
not_ = ~( COL2 > 7 );
datalines;
11.21 5.3
3.11  11.4
;
PROC PRINT DATA=MYDATA1;
RUN;

After running the above code, we can get the output below.

SAS operator

Comparison operator

The following table describes the details of the comparison operator. T hese operators compare the values of variables, resulting in true values, 1 for TRUE, and 0 for False. Let's assume that the two data variables V1 and V2 have values of 8 and 4, respectively.

The operator Describe Cases
= Equal operator. If the two data values are equal, the result is 1, otherwise 0. (V1 s 8) gives 1.
^ = NOT EQUAL operator. If the two data values are not equal, the result is 1, otherwise 0. Gives 1 .
< LESS THAN operator. (V2 slt;V2) provides 1.
<= Less than or equal to the operator. (V2 slt; s 4) gives 1.
> Is greater than the operator. (V2; V1) gives 1.
> = The operator is greater than or equal to . (V2, V1) gives 0.
In The IN operator. If the value of the variable is equal to any one of the values in the list of given values, 1 else is returned or 0 is returned. V1 gives 1 in (5,7,9,8).

Cases

DATA MYDATA1;
input @1 COL1 5.2	@7 COL2 4.1; 
EQ_ = (COL1 = 11.21);
NEQ_= (COL1 ^= 11.21);
GT_ = (COL2 => 8);
LT_ = (COL2 <= 12);
IN_ = COL2 in( 6.2,5.3,12 );
datalines;
11.21 5.3
3.11  11.4
;
PROC PRINT DATA=MYDATA1;
RUN;

After running the above code, we can get the output below.

SAS operator

Min/Max operator

The following table describes the details of the minimum/maximum operator. These operators compare the values of variables in a row and return the minimum or maximum values in the list of values in the row.

The operator Describe Cases
MIN MIN operator. It returns the minimum value from the list of values in the row. MIN (45.2, 11.6, 15.41) gave 11.6
MAX MAX operator. It returns the maximum value from the list of values in the row. MAX (45.2, 11.6, 15.41) gave 45.2

Cases

DATA MYDATA1;
input @1 COL1 5.2	@7 COL2 4.1 @12 COL3 6.3; 
min_ = MIN(COL1 , COL2 , COL3);
max_ = MAX( COL1, COl2 , COL3);
datalines;
11.21 5.3 29.012
3.11  11.4 18.512
;
PROC PRINT DATA=MYDATA1;
RUN;

After running the above code, we can get the output below.

SAS operator

The connection operator

The following table describes the details of the Concatenation operator. T his operator connects two or more string values. Returns a single character value.

The operator Describe Cases
|| The connection operator. It returns a connection of two or more values. ' Hello '||' World' outputs Hello World

Cases

DATA MYDATA1;
input  COL1 $	COL2 $  COL3 $; 
concat_ = (COL1 || COL2 || COL3);
datalines;
Tutorial s point
simple easy learning
;
PROC PRINT DATA=MYDATA1;
RUN;

After running the above code, we can get the output below.

SAS operator

Operator priority

Operator precedence represents the order in which multiple operators exist in complex expressions. The following table describes the order of precedence in a set of operators.

Group Order Symbol
The first group Right to left ** + - NOT MIN MAX
The second group Left to right * /
The third group Left to right + -
The fourth group Left to right ||
The fifth group Left to right <<= => =>