Home

SAS For Dummies Cheat Sheet

|
Updated:  
2025-04-14 16:47:09
|
From The Book:  
Data Science Essentials For Dummies
Explore Book
Buy On Amazon

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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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

  1. 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 input statement 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, the dollar6.2 informat tells SAS how to interpret the value (six places in total, with two decimal places and room for the currency symbol).
  2. 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 input statement:
    data convert;
       length dateval 8;
       dateval = input("10JAN2025",date9.);
    run;
  3. Calculate a new date value relative to a given date. When the date value is a number, you can use the intnx and intck functions 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;
  4. Generate a random number. The rand function 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);
  5. 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 %sysfunc macro function to invoke a SAS function anywhere in open code.  For example, to show the current time in a title statement, use something like this:
    title "Result generated at %sysfunc(time(),timeampm.)";
    The %sysfunc function also lets you apply a SAS format to the result (timeampm, in this example).

Handy utility procedures in SAS

Though SAS has dozens of analytical and reporting procedures that get all the glory, don’t forget the essential utility procedures that help set you up for success and manage your SAS session:
  1. proc datasets This 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 use proc copy and proc contents for some of these tasks, the proc datasets procedure does it all.
  2. proc delete This 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.
  3. proc options This procedure lists all the current values of your SAS system options. SAS option settings can affect how your programs run, so proc options is essential for understanding SAS session behavior.
  4. proc format Though 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 the format procedure to create user-defined formats for your SAS tasks.
  5. proc sort Need your data records to be in a certain order or grouped into categories? The proc sort procedure 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.