Functions in SAS

Last updated on Sep 14 2022
Nitin Pawar

Table of Contents

Functions in SAS

SAS has a wide variety of in-built functions which help in analysing and processing the data. These functions are used as part of the DATA statements. They take the data variables as arguments and return the result which is stored into another variable. Depending on the type of function, the number of arguments it takes can vary. Some functions accept zero arguments while some other accept fixed number of variables. Below is a list of types of functions SAS provides.

Syntax

The general syntax for using a function in SAS is as below.

FUNCTIONNAME(argument1, argument2...argumentn)

Here the argument can be a constant, variable, expression or another function.

Function Categories

Depending on their usage, the functions in SAS are categorised as below.

• Mathematical

• Date and Time

• Character

• Truncation

• Miscellaneous

Mathematical Functions

These are the functions used to apply some mathematical calculations on the variable values.

Examples

The below SAS program shows the use of some important mathematical functions.

data Math_functions;

v1=21; v2=42; v3=13; v4=10; v5=29;

/* Get Maximum value */

max_val = MAX(v1,v2,v3,v4,v5);

/* Get Minimum value */

min_val = MIN (v1,v2,v3,v4,v5);

/* Get Median value */

med_val = MEDIAN (v1,v2,v3,v4,v5);

/* Get a random number */

rand_val = RANUNI(0);

/* Get Square root of sum of the values */

SR_val= SQRT(sum(v1,v2,v3,v4,v5));

proc print data = Math_functions noobs;

run;

When the above code is run, we get the following output −

sas 12

Date and Time Functions

These are the functions used to process date and time values.

Examples

The below SAS program shows the use of date and time functions.

data date_functions;

INPUT @1 date1 date9. @11 date2 date9.;

format date1 date9. date2 date9.;

/* Get the interval between the dates in years*/

Years_ = INTCK('YEAR',date1,date2);

/* Get the interval between the dates in months*/

months_ = INTCK('MONTH',date1,date2);

/* Get the week day from the date*/

weekday_ = WEEKDAY(date1);

/* Get Today's date in SAS date format */

today_ = TODAY();

/* Get current time in SAS time format */

time_ = time();

DATALINES;

21OCT2000 16AUG1998

01MAR2009 11JUL2012

;

proc print data = date_functions noobs;

run;

When the above code is run, we get the following output −

sas 13

Character Functions

These are the functions used to process character or text values.

Examples

The below SAS program shows the use of character functions.

data character_functions;

/* Convert the string into lower case */

lowcse_ = LOWCASE('HELLO');

/* Convert the string into upper case */

upcase_ = UPCASE('hello');

/* Reverse the string */

reverse_ = REVERSE('Hello');

/* Return the nth word */

nth_letter_ = SCAN('Learn SAS Now',2);

run;

proc print data = character_functions noobs;

run;

When the above code is run, we get the following output −

sas 14

Truncation Functions

These are the functions used to truncate numeric values.

Examples

The below SAS program shows the use of truncation functions.

data trunc_functions;

/* Nearest greatest integer */

ceil_ = CEIL(11.85);

/* Nearest greatest integer */

floor_ = FLOOR(11.85);

/* Integer portion of a number */

int_ = INT(32.41);

/* Round off to nearest value */

round_ = ROUND(5621.78);

run;

proc print data = trunc_functions noobs;

run;

When the above code is run, we get the following output −

sas 15

Miscellaneous Functions

Let us now understand the miscellaneous functions of SAS with some examples.

Examples

The below SAS program shows the use of Miscellaneous functions.

data misc_functions;

/* Nearest greatest integer */

state2=zipstate('01040');

/* Amortization calculation */

payment = mort(50000, . , .10/12,30*12);

proc print data = misc_functions noobs;

run;

When the above code is run, we get the following output −

sas 16

So, this brings us to the end of blog. This Tecklearn ‘Functions in SAS’ blog helps you with commonly asked questions if you are looking out for a job in SAS. If you wish to learn SAS and build a career in Data Analytics domain, then check out our interactive, SAS Training for SAS BASE Certification Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

SAS Training for SAS BASE Certification

SAS Training for SAS BASE Certification Training

About the Course

SAS Certification Training is intended to make you an expert in SAS programming and Analytics. You will be able to analyse and write SAS code for real problems, learn to use SAS to work with datasets, perform advanced statistical techniques to obtain optimized results with Advanced SAS programming. In this SAS online training course, you will also learn SAS macros, Machine Learning, PROC SQL, procedure, statistical analysis and decision trees. You will also work on real-life projects and prepare for the SAS Certified Base Programmer certification exam. Upon the completion of this SAS online training, you will have enough proficiency in reading spreadsheets, databases, using SAS functions for manipulating this data and debugging it.

Why Should you take SAS Training?

• The average salary for a Business Intelligence Developer skilled in SAS is $100k (PayScale salary data)

• SAS, Google, Facebook, Twitter, Netflix, Accenture & other MNCs worldwide are using SAS for their Data analysis activities and advance their existing systems.

• SAS is a Leader in 2017 Gartner Magic Quadrant for Data Science Platform.

What you will Learn in this Course?

Introduction to SAS

• Introduction to SAS

• Installation of SAS

• SAS windows

• Working with data sets

• Walk through of SAS windows like output, search, editor etc

SAS Enterprise Guide

• How to read and subset the data sets

• SET Statement

• Infile and Infile Options

• SAS Format -Format Vs Informat

SAS Operators and Functions

• Using Variables

• Defining and using KEEP and DROP statements

• Output Statement

• Retain Statement

• SUM Statement

Advanced SAS Procedures

• PROC Import

• PROC Print

• Data Step Vs Proc

• Deep Dive into Proc

Customizing Datasets

• SAS Arrays

• Useful SAS Functions

• PUT/INPUT Functions

• Date/Time Functions

• Numeric Functions

• Character Functions

SAS Format and SAS Graphs

• SAS Format statements

• Understanding PROC GCHART, various graphs, bar charts: pie, bar

Sorting Techniques

• NODUP

• NODUKEY

• NODUP Vs NODUKEY

Data Transformation Function

• Character functions, numeric functions and converting variable type

• Use functions in data transformation

Deep Dive into SAS Procedures, Functions and Statements

• Find Function

• Scan Function

• MERGE Statement

• BY Statement

• Joins

• Procedures Vs Function

• Where Vs If

• What is Missover

• NMISS

• CMISS

PROC SQL

• SELECT statement

• Sorting of Data

• CASE expression

• Other SELECT statement clauses

• JOINS and UNIONS

Using SAS Macros

• Benefits of SAS Macros

• Macro Variables

• Macro Code Constituents and Macro Step

• Positional Parameters to Macros

Got a question for us? Please mention it in the comments section and we will get back to you.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0 responses on "Functions in SAS"

Leave a Message

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