Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 37 ยท Environment and Data Steps

SAS Functions and Dates: Numeric, Character and Date Functions

Learn the SAS functions you will use every day: numeric and character functions, how SAS stores dates as days since 1960, and MDY, YEAR, TODAY, INTCK and INTNX.

SAS ships with hundreds of functions, ready-made routines you call inside a DATA step or in PROC SQL: name(arguments). This lesson covers the groups you will use most: numeric, character and date functions. Dates deserve their own section because SAS stores them in a way that surprises newcomers.

Numeric functions

Function Result
sum(a, b, c) Sum, ignoring missing values
mean(a, b, c) Mean of the non-missing arguments
min(...), max(...) Smallest or largest non-missing value
n(...), nmiss(...) Count of non-missing / missing arguments
round(x, 0.01) Round to the nearest 0.01
int(x), floor(x), ceil(x) Truncate, round down, round up
abs(x), sqrt(x), log(x), exp(x) Usual maths (log is the natural log)
mod(a, b) Remainder
SAS
data scores;
   input math reading science;
   total = sum(math, reading, science);
   avg   = round(mean(math, reading, science), 0.1);
   done  = n(math, reading, science);
   datalines;
90 85 .
72 . 80
;
run;

Compare sum(math, reading) with math + reading: the function treats a missing value as "nothing to add", while the operator gives missing. The same distinction applies to mean versus (a + b) / 2. Functions also accept variable lists: sum(of q1-q4) or mean(of score1-score10). The word of is required.

Character functions

Function Purpose Example result
upcase, lowcase, propcase Change case propcase("ana LEE") is Ana Lee
strip(s) Remove leading and trailing blanks
length(s) Length without trailing blanks
substr(s, start, len) Extract part of a string substr("SAS2024", 4, 4) is 2024
scan(s, n, delims) The n-th word scan("a,b,c", 2, ",") is b
index(s, "x"), find(s, "x") Position of text; 0 if absent
cat, cats, catt, catx Join strings catx("-", "A", "B") is A-B
compress(s, chars) Remove characters
tranwrd(s, old, new) Replace text
translate, reverse, repeat Other string tools

catx strips blanks from each piece and puts a delimiter between them, so it is usually the best way to join values. The || operator does not remove blanks and often leaves unwanted spaces.

SAS
data people;
   length full $ 40;
   first = "Ana"; last = "Lee";
   full  = catx(" ", first, last);          /* Ana Lee */
   initials = cats(substr(first,1,1), substr(last,1,1));  /* AL */
run;

Convert between types deliberately with input() (character to number) and put() (number to character):

SAS
amount = input("1,250", comma8.);    /* numeric 1250 */
txt    = put(amount, 8.);            /* character */

Letting SAS convert for you works but writes a note to the log; explicit conversion avoids surprises.

How SAS stores dates

A SAS date value is the number of days since 1 January 1960. That day is 0; the day before is -1; and so on. A datetime value is the number of seconds since midnight on 1 January 1960. A time value is seconds since midnight.

Because dates are plain numbers, you can add and subtract them: due = invoice_date + 30; is thirty days later, and end - start is the number of days between. They display as readable dates only when you attach a format such as date9. or yymmdd10.; without one, you see the number.

Creating dates

SAS
data dates;
   d1 = mdy(3, 15, 2024);               /* from month, day, year */
   d2 = "15MAR2024"d;                   /* date literal */
   d3 = input("2024-03-15", yymmdd10.); /* from text with an informat */
   today = today();
   format d1 d2 d3 today date9.;
run;

The date literal "15MAR2024"d is written as day, three-letter month and four-digit year in quotes, followed by d.

Pulling out parts

Function Gives
year(d), month(d), day(d) Parts of the date
weekday(d) 1 = Sunday ... 7 = Saturday
qtr(d) Quarter 1 to 4
datepart(dt) Date from a datetime

Counting and shifting intervals

INTCK counts how many interval boundaries are crossed between two dates; INTNX moves a date forward or back by intervals.

SAS
months = intck("month", "31JAN2024"d, "01FEB2024"d);   /* 1 */
next_month_start = intnx("month", "15MAR2024"d, 1);    /* 01APR2024 */
month_end = intnx("month", "15MAR2024"d, 0, "end");    /* 31MAR2024 */

The first example is 1 even though only one day passed, because one month boundary (the start of February) was crossed. That is how intck counts: it is not elapsed time. For age in whole years, use yrdif(birth, today, "age") or intck("year", birth, today, "continuous"), which counts completed years.

A date calculator

This Python stand-in (not SAS) shows the 1960 epoch and the boundary-counting behavior of INTCK.

Python Python stand-in: SAS date numbers and INTCK

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Never store dates as text such as "15/03/2024": they cannot be sorted or subtracted. Convert once with input() and an informat, then use a date format for display.

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What number is the SAS date value of 1 January 1960?

    SAS dates count days from 1 January 1960, which is day 0.
  2. 2. What is the difference between sum(a, b) and a + b when b is missing?

    Functions like sum, mean, min and max ignore missing arguments; operators propagate missing.
  3. 3. What does intck('month', '31JAN2024'd, '01FEB2024'd) return?

    intck counts boundaries crossed. The start of February lies between the two dates.
  4. 4. Which function joins strings, strips blanks and inserts a delimiter?

    catx strips each piece and puts the delimiter you give between them.
  5. 5. How do you make a date value display as 15MAR2024?

    A format changes how the number is displayed without changing its value.

Summary

  • Functions take arguments in parentheses; sum, mean, min, max ignore missing values and accept of variable lists.
  • Common character functions: substr, scan, index/find, catx, strip, upcase, compress, tranwrd.
  • A SAS date is the number of days since 1 January 1960; datetimes are seconds since that midnight.
  • Create dates with mdy, date literals ("15MAR2024"d) or input with an informat; display them with formats.
  • intck counts boundaries crossed; intnx moves dates by intervals.

Next: Reading Instream Data with DATALINES and INPUT