Functions in SAS – Part 2 (Numeric and Date Functions)

Welcome back to Mindful Data Minds! In the previous session, we explored character functions in SAS. Now, let’s move on to numeric functions and date functions, along with the important PUT and INPUT conversions.

Watch the Full Tutorial

What You Will Learn

  • How to use SUM and MEAN row-wise.
  • How to round numbers with ROUND.
  • How to convert between numeric and character using PUT and INPUT.
  • How SAS stores dates and how to format them.
  • How to extract day, month, quarter, year, and weekday.
  • How to calculate date differences with INTCK.
  • How to shift dates with INTNX.

Numeric Functions

data a;
  set baseball_numeric;
  s1=sum(nAtBat,nHome);
  s2=sum(of nAtBat--nAssts);

  m1=mean(nAtBat,nHome);
  m2=mean(of nAtBat--nAssts);

  r1=round(logSalary,0.1);
  r2=round(logSalary,0.01);
  r3=round(logSalary,0.001);
  r4=round(logSalary,0.0001);
  r5=round(logSalary,1.0);
  r6=round(logSalary,10.0);

  num_to_char = put(nAtBat,$8.);
  char_to_num = input(num_to_char,8.);
run;

SUM Function

Adds values row by row.

s1=sum(nAtBat,nHome);

Example: If nAtBat=293 and nHome=1, result = 294.

You can also sum across a range of variables:

s2=sum(of nAtBat--nAssts);

This adds all variables between nAtbat and nAssts.

Note: Unlike SQL, SAS functions like SUM and MEAN work row-wise, not across the entire column.

MEAN Function

Calculates the average of variables.

m1=mean(nAtBat,nHome);
m2=mean(of nAtBat--nAssts);

Example: Mean of natbat=293 and nhome=1 = 147.

ROUND Function

Rounds numeric values to specified precision.

r1=round(logSalary,0.1); /* One decimal */
r2=round(logSalary,0.01); /* Two decimals */
r3=round(logSalary,0.001); /* Three decimals */
r4=round(logSalary,0.0001); /* Four decimals */
r5=round(logSalary,1.0); /* Nearest integer */
r6=round(logSalary,10.0); /* Nearest multiple of 10 */

Example: 6.163 rounded to 0.1 = 6.2, rounded to 10 = 10.

PUT and INPUT Functions

  • PUT → Converts numeric to character.
  • INPUT → Converts character to numeric.
num_to_char = put(nAtBat,$8.);
char_to_num = input(num_to_char,8.);

Example: natbat=123"123" (character) → back to 123 (numeric).

Date Functions

Dates in SAS are stored as numbers (days since 1 Jan 1960). Formats make them human-readable.

data b;
  format dt1 tdy1 tdy2 ftr_dt mdy date9.;
  dt1="25JAN2020"d;
  tdy1=date();
  tdy2=today();
  day=day(dt1);
  month=month(dt1);
  quarter = qtr(dt1);
  wday=weekday(dt1);
  year=year(dt1);

  mdy=mdy(month,day,year);
  diff = intck("months",dt1,tdy1);

  ftr_dt = intnx("month",tdy1,0,"B");
run;

Current Date

tdy1=date();
tdy2=today();

Both return today’s date.

Extracting Components

dt1="25JAN2020"d;
day=day(dt1); /* Day of month */
month=month(dt1); /* Month */
quarter = qtr(dt1); /* Quarter */
wday=weekday(dt1); /* 1=Sunday … 7=Saturday */
year=year(dt1); /* Year */

Creating Dates

mdy=mdy(month,day,year); /* 25 Jan 2020 */

Date Differences

diff = intck("months",dt1,tdy1);

Example: Difference between Jan 2020 and Aug 2023 = 43 months.

Date Shifting

ftr_dt = intnx("month",tdy1,0,"B");

Returns the first day of the current month.

Formatting Dates

To display dates properly:

format dt1 tdy1 tdy2 ftr_dt mdy date9.;

Example: 25JAN2020, 01AUG2023.

Next Step

Continue learning by exploring the next tutorial in this series. Also subscribe to get notified about new lessons.

Have a Question?

Drop your doubts in the comments below or contact us.

Leave a Reply

Your email address will not be published. Required fields are marked *