Variables, Types, and Missing Values
Prerequisites: Reading data with INPUT
SAS has just two data types:
- Numeric — stored as 8-byte floating-point numbers. Dates and times are numeric too.
- Character — fixed-length text, padded with blanks to the variable's length (1 to 32,767 bytes).
A variable's type and length are fixed the first time SAS sees it in a step.
Missing values
A missing numeric value is written . and a missing character value is a
blank. Arithmetic with a missing value gives missing — but SAS
functions like SUM and MEAN skip missing values:
data totals;
input name $ test1 test2;
total = test1 + test2;
total2 = sum(test1, test2);
datalines;
Ada 90 85
Alan . 70
;
run;
proc print data=totals;
run;
Obs name test1 test2 total total2
1 Ada 90 85 175 175
2 Alan . 70 . 70
And the log warns you:
NOTE: Missing values were generated as a result of performing an operation on missing values.
Neither result is "right" — decide whether a missing test should count as
zero (SUM) or make the total unknown (+).
Missing is smaller than every number
In comparisons, a missing numeric value is lower than any number, including negative ones. That catches everyone once:
data check;
input name $ score;
length status $ 4;
if score < 50 then status = 'fail';
else status = 'pass';
datalines;
Ada 72
Alan .
Grace 41
;
run;
proc print data=check;
run;
Obs name score status
1 Ada 72 pass
2 Alan . fail
3 Grace 41 fail
Alan has no score, yet he "failed". Test for missing values first:
if missing(score) then status = 'n/a';
else if score < 50 then status = 'fail';
else status = 'pass';
MISSING() works for both numeric and character variables.
Automatic type conversion
If you use a character value where a number is expected (or the reverse), SAS converts it and writes a note:
data _null_;
x = '42' + 1;
put x=;
run;
Log (abridged):
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
x=43
It works, but treat that note as a bug to fix: convert explicitly with
INPUT() and PUT() (covered in Level 2), so your intent is clear.
Practice
Read three products with a price and a discount, where one discount is missing. Compute the final price so that a missing discount counts as no discount.
Show solution
data prices;
input product $ price discount;
final = price - sum(discount, 0);
datalines;
Lamp 40 5
Desk 150 .
Chair 60 10
;
run;
proc print data=prices;
run;
Obs product price discount final
1 Lamp 40 5 35
2 Desk 150 . 150
3 Chair 60 10 50
sum(discount, 0) is 0 when the discount is missing.