Assignment Statements and Operators
Prerequisites: Variables, types, and missing values
An assignment statement creates or updates a variable:
variable = expression;.
Arithmetic
data _null_;
a = 17;
b = 5;
sum = a + b;
diff = a - b;
prod = a * b;
quot = a / b;
power = 2 ** 10;
remainder = mod(a, b);
put sum= diff= prod= quot= power= remainder=;
run;
sum=22 diff=12 prod=85 quot=3.4 power=1024 remainder=2
**is exponentiation./always gives a full result — there's no integer division operator; useint(a / b)orfloor(a / b).- Parentheses control the order, as in math.
Comparisons and logic
| Symbol | Mnemonic | Meaning |
|---|---|---|
= |
EQ |
equal |
^= or ~= |
NE |
not equal |
> < |
GT LT |
greater / less than |
>= <= |
GE LE |
greater / less or equal |
& |
AND |
both true |
| |
OR |
either true |
^ or ~ |
NOT |
negation |
IN |
in a list |
A comparison evaluates to 1 (true) or 0 (false), which you can store
or add up:
data flags;
set sashelp.class;
tall = (height > 65);
teen = age in (13, 14, 15, 16, 17, 18, 19);
keep name height age tall teen;
run;
proc print data=flags(obs=4);
run;
Obs Name Age Height tall teen
1 Alfred 14 69.0 1 1
2 Alice 13 56.5 0 1
3 Barbara 13 65.3 1 1
4 Carol 14 62.8 0 1
(obs=4) is a data set option: it limits the procedure to the first
four rows.
Joining text
|| concatenates, but keeps the trailing blanks that pad character
variables. The CAT family trims for you — CATX also inserts a
separator:
data _null_;
length first last $ 10;
first = 'Ada';
last = 'Lovelace';
joined = first || last;
nice = catx(' ', first, last);
put joined=;
put nice=;
run;
joined=Ada Lovelace
nice=Ada Lovelace
first is 10 characters long, so 'Ada' is stored as 'Ada' plus 7
blanks — and || keeps them.
Practice
Compute the body-mass index for the first three students in
SASHELP.CLASS: bmi = 703 * weight / height**2 (weight in pounds,
height in inches), rounded to one decimal with round(x, 0.1).
Show solution
data bmi;
set sashelp.class(obs=3);
bmi = round(703 * weight / height**2, 0.1);
keep name height weight bmi;
run;
proc print data=bmi;
run;
Obs Name Height Weight bmi
1 Alfred 69.0 112.5 16.6
2 Alice 56.5 84.0 18.5
3 Barbara 65.3 98.0 16.2