The field of SAS and SAS programming has evolved over nearly 50 years, leading to the development of various shorthand techniques. These techniques may not be immediately apparent to new SAS users, but they become clear with learning and practice. Use the tips here to get a head-start and accelerate your initiation to SAS.
5 important things to know about the SAS programming language
- SAS isn’t just a programming language. SAS is a set of capabilities. Unlike many other programming languages, SAS has built-in procedures and functions that are designed to work with data and statistics. Other languages, like Python or Java, require additional code libraries and packages to add the capabilities you want. With SAS, it’s all there; you just need to know where to look.
- SAS is a 4GL. The SAS programming language is known as a fourth-generation language, which means that it provides a higher level of abstraction than functional or object-oriented languages like C or Java. The built-in statements and procedures reduce the number of lines of code and logic you need to create to accomplish your tasks.
- SAS DATA step runs in two phases: compile time and run time. During compile time, SAS performs a syntax check, converts the code to machine language for fast execution, determines data types and lengths, and creates the program data vector (PDV). During execution time, SAS reads and processes data, performs calculations, and evaluates conditional logic and then writes any output data or files. Knowing how these two phases work can help you write and debug effective SAS programs.
- SAS macro statements are processed and resolved first. You can think of the SAS macro language as a text generator: It’s code that can generate — in a few lines of logic and loops — much more code that will then compile and run during the execution phase. It’s powerful and perilous at the same time. That’s why it’s best to get your core code working first and then add in any SAS macro logic you want for conditions and loops.
- There are multiple ways to do almost anything in SAS. As a programming language, SAS is over 50 years old, and the SAS syntax from decades ago still works today. But for most jobs, there are also new and better ways to accomplish that work. Be sure to explore the newer methods before settling into a technique you copied from a SAS conference paper from 1995.
5 essential SAS programming techniques
- Convert a character value to a numeric value in DATA step.
This is the most common early task for new SAS users. Your data may have character values that look like numbers but are not represented as truly numeric, so you cannot use them in computations. To convert them, simply use the
inputstatement to read and interpret the value with a SAS informat, which indicates how SAS should read it:data convert; length numvar 8; numvar = input("$100.99",dollar6.2); run;In this example, thedollar6.2informat tells SAS how to interpret the value (six places in total, with two decimal places and room for the currency symbol). - Convert a character value to a date value.
This is just a tricky special case of the first tip because, in SAS, a date value is a number. To convert to a date, you apply a date informat in the
inputstatement:data convert; length dateval 8; dateval = input("10JAN2025",date9.); run; - Calculate a new date value relative to a given date.
When the date value is a number, you can use the
intnxandintckfunctions to perform “date math.” For example, this program computes a new date that is 60 days from a start date and then computes how many Mondays occur during that interval:data convert; length datevar 8 nextdate 8; format datevar date9. nextdate date9.; datevar = '01JAN2025'd; nextdate = intnx('day',datevar,60); mondays = intck('week1.1',datevar,nextdate); run; - Generate a random number.
The
randfunction is a one-stop shop for generating random numbers with any distribution or range. For example, to generate a random number between 1 and 100, use:x = rand('integer',1,100); - Execute a SAS function outside of a DATA step.
SAS functions are commonly used to calculate and transform values in a DATA step. You can use the
%sysfuncmacro function to invoke a SAS function anywhere in open code. For example, to show the current time in atitlestatement, use something like this:title "Result generated at %sysfunc(time(),timeampm.)";
The%sysfuncfunction also lets you apply a SAS format to the result (timeampm, in this example).
Handy utility procedures in SAS
proc datasetsThis procedure can describe the contents of your SAS libraries and datasets, copy data members from one library to another, modify data attributes such as variable names and formats, and much more. Though you can useproc copyandproc contentsfor some of these tasks, theprocdatasetsprocedure does it all.proc deleteThis procedure efficiently deletes a SAS data set you no longer need. Use it to clean up your SAS session or to recover space in your environment.proc optionsThis procedure lists all the current values of your SAS system options. SAS option settings can affect how your programs run, soproc optionsis essential for understanding SAS session behavior.proc formatThough SAS offers hundreds of useful SAS formats and informats to help you control how SAS reads and displays your data, sometimes you need something customized. Use theformatprocedure to create user-defined formats for your SAS tasks.proc sortNeed your data records to be in a certain order or grouped into categories? Theproc sortprocedure can sort your records in ascending or descending order, by as many variables as you want. Some SAS procedures and BY-group processing assumes that data is in a sorted order. But use caution: Sorting data is an expensive I/O operation and can take a long time and use a lot of temporary “scratch” space as it runs.
Learn more about these procedures, and all the SAS syntax referenced in this Cheat Sheet, by visiting https://documentation.sas.com.
About This Article
This article is from the book:
About the book author:
Chris Hemedinger works in SAS R&D on the team that builds SAS Enterprise Guide, a popular user interface for SAS customers.
This article can be found in the category:
Hot off the press
Explore Related content
General Data Science
Articles
Linear Regression vs. Logistic Regression
Wondering how to differentiate between linear and logistic regression? Learn the difference here and see how it applies to data science.
General Data Science
Cheat Sheet
Data Analytics & Visualization All-in-One Cheat Sheet
Boost your data analytics and visualization with our all-in-one cheat sheet. Learn about essential tools like Microsoft Power BI, Tableau, SQL, and more.
General Data Science
Cheat Sheet
Decision Intelligence For Dummies Cheat Sheet
Learn about the most important aspects of the decision intelligence approach, which puts human beings back into the decision loop.
General Data Science
Cheat Sheet
Microsoft Power BI For Dummies Cheat Sheet
Here's a handy guide with important features and aspects of Microsoft Power BI. Keep it by your side as you're learning the platform.
General Data Science
Articles
Laws and Regulations You Should Know for Blockchain Data Analysis Projects
Anytime you start a data analysis project, you need to be aware of certain regulations and data privacy laws. Here's what you need to know.
General Data Science
Articles
Aligning Blockchain Data with Real-World Business Processes
How can you apply blockchain data to real-world business processes? Use this brief guide to discover how to apply your information.
General Data Science
Articles
An Intro to Aligning Blockchain Data Analytics with Business Goals
Want to learn how to align your blockchain data analysis with your business goals? Use this brief intro to discover the best approach.
General Data Science
Articles
Blockchain Use Cases
Blockchain has so much potential. Ever wonder how it can be used in real life? Check out these blockchain use cases, from Dummies.com.
General Data Science
Articles
Fitting Blockchain into Today’s Business Processes
Blockchain is a great technology with trememndous potential, but it needs to be able to fit into today's business processes. Learn more.
General Data Science
Articles
A Quick Comparison of Blockchain Data Analytics Toolsets and Frameworks
Check out this quick comparison of blockchain data analytics frameworks to learn the benefits of each at a glance, from Dummies.com.
General Data Science
Articles
The Primary Types of Blockchain
Have you ever wondered what the different types of blockchain look like? Use this guide from Dummies.com to learn how they compare.
General Data Science
Articles
10 Tools for Developing Blockchain Analytics Models
Want to learn how to improve your blockchain analytics models? Check out these ten tools to make better models, from Dummies.com.
General Data Science
Articles
10 Tips for Visualizing Blockchain Data
Your data should help you tell a story. Use these ten tips to enhance your blockchain data visualizations, from Dummies.com.
General Data Science
Articles
10 Uses for Blockchain Analytics
Are you curious to learn how blockchain data analytics can be used? Check out this list of top ten uses, from Dummies.com.
General Data Science
Cheat Sheet
Blockchain Data Analytics For Dummies Cheat Sheet
To best leverage blockchain data analytics, become familiar with blockchain technology, how it stores data, and how to extract and analyze data.
General Data Science
Articles
How to Perform Pattern Matching in Python
Python is an ideal language for data science. Find out how to use Python to perform pattern matching for data science applications.
General Data Science
Articles
How Data is Collected and Why It Can Be Problematic
Data collection seems like a precise science. Learn how data is collected and dicover why those processes can be problematic for data scienteists.
General Data Science
Articles
The Need for Reliable Sources in Data Science Applications
When programming for data science, the entire outcome is based on the reliability of a data source. See how data science is applied in real-life examples.
General Data Science
Articles
How Pattern Matching Works in Data Science
Pattern matching is extremely useful for machine learning, depe learning, and AI applications. Find out how pattern matching works.
General Data Science
Articles
The Basics of Deep Learning Framework Usage and Low-End Framework Options
Deep learning frameworks allows you to develop complex and sophisticated applications. You also explore low-end deep learning frameworks such as Pytorch.
Where to find
Available at these book sellers across the United States.

SAS For Dummies














