BookmarkSubscribeRSS Feed

Using the DATA Step Debugger in SAS Studio on SAS Viya

Started ‎03-26-2026 by
Modified ‎03-26-2026 by
Views 545

When I first learned to write DATA step code, I managed to get by using sample code and guess-and-check strategies until things worked. I shudder to think of the logic errors I likely (unknowingly) had in my code! But I can pinpoint the very day when that all changed - when I learned how the DATA step is compiled and executed behind the scenes. It was no longer a mysterious black box, but a powerful and logical process that I could control to solve the most complex data manipulation challenges.

 

DATA Step Magic and the Program Data Vector

 

The magic behind the DATA step is the Program Data Vector (PDV). The PDV is a temporary memory area that SAS uses to build each observation (row) during a DATA step. It holds the values of all variables as they are read, calculated, and modified before being written to the output dataset. Each time the DATA step loops, the PDV is reset and reused for the next observation. If you understand the rules and behavior of the PDV, then you will be the master of the DATA step!

 

So how can you watch what happens in the PDV behind the scenes as your DATA step code executes? The DATA step debugger in SAS Studio on SAS Viya gives us a window to observe the process, line-by-line in the code and row-by-row in the data.

 

NOTE: The interactive DATA step debugger is available in select versions of SAS Studio on SAS Viya and in SAS Enterprise Guide. This blog focuses on the debugger that was reintroduced in SAS Studio in the SAS Viya 2026.01 stable release.

 

Syntax Errors versus Logic Errors

 

In SAS, syntax errors happen when code breaks the language rules (like missing semicolons or misspelled keywords), and SAS usually stops or flags them right away with messages in the log.

 

Logic errors occur when the code runs but produces incorrect results due to flawed conditions or reasoning, making them harder to spot. This is where the debugger can save you!

 

Let's look at a sample program. The goal of this DATA step is to read only Audi cars from the SASHELP.CARS table. If the first letter in the value of Model is A, then we want to create a new variable named Series and assign the value A. Otherwise, assign the value Other.

 

data Audi;
    set sashelp.cars(keep=Make Model MSRP);
    where Make="Audi";
    SeriesCode=substr(Model,1,1);
    if SeriesCode="A" then Series="A";
    else Series="Other";
    keep Make Model Series MSRP;
run;

 

When we run this step, there are no syntax errors - just pretty blue notes in the log. But looking at the output, something is not quite right. Every row is assigned a value of O, instead of either A or Other.

 

stever_1_AudiOutput1.jpg

 

 

What is the DATA step thinking?

 

The DATA step debugger allows us to get inside SAS's brain. In the Program tab in SAS Studio, click Debug. This will enable the cute little green bug icon next to any DATA steps. Simply click the bug to launch the debugger.

 

stever_2_LaunchDebugger.jpg

 

The DATA step debugger opens and we're ready to dive in. There are three main sections in the tool.

 stever_3_DebuggerLayout-1024x604.jpg

 

  1. The code panel allows you to view the DATA step and control execution flow. You can:
    1. Execute one statement at a time stever_StepOver1.png 
    2. Set breakpoints to pause execution at a specific line stever_Breakpoint2.png 
    3. Continue running the program stever_Play3.png 
    4. Stop running the program stever_Stop4.png 
  2. The variable panel shows the contents of the PDV. As you move through the code, the debugger continuously updates the values of variables in memory, so you can see exactly what’s happening at each step.
    1. Variable values that changed with the previous statement are displayed in red.
    2. You can select the Watch checkbox to monitor any variable in the program. SAS suspends execution of the program whenever the value of a watched variable changes.
  3. The console allows you to enter commands directly in the command line.
    1. To see all available commands, click List of commands.
    2. For additional details about DATA step debugger commands, refer to Debugger Commands Overview in SAS Code Debugger: User’s Guide.
    3. As an example, you can set a conditional breakpoint so the program pauses only when a specific condition is true: break 4 when series="O"

 

Identifying the Problem

 

Let's work through our program line by line to diagnose the logic error(s).

  1. Click Step execution for next line   stever_StepOver1.png   to execute the SET statement. The first row read from the SASHELP.CARS table satisfies the WHERE condition of Make is Audi. Notice the value of Model is A4 1.8T 4dr, but values have not yet been assigned to SeriesCode or Series.

    stever_4_Example1.jpg

     

  2. The SeriesCode statement is highlighted, meaning it is the next statement to be executed. Click Step execution for next line  stever_StepOver1.png , and notice no visible value is assigned to the variable, SeriesCode, which is unexpected. The intent of our code is to assign a value of A.

    stever_5_Example2.jpg

     

  3. With the IF statement highlighted, click Step execution for next line  stever_StepOver1.png.  The next highlighted line is the ELSE statement, indicating that the IF condition was false, so the ELSE statement executes next.

    stever_6_Example3.jpg

     

  4. Click Step execution for next line   stever_StepOver1.png   and confirm that the value O, rather than Other, is assigned to Series.

    stever_7_Example4.jpg

  5. You can continue to step through the program and confirm that the ELSE statement executes for each row read into the PDV, assigning a value of O.

Based on our observations in the debugger, we can identify there are two issues:

  • The SUBSTR function is not returning the first letter as we would expect.
  • The value of Other is truncated to only assign the first letter, O.

These clues help to pinpoint the placement of the logic errors - hopefully providing enough information to solve the issues. In this scenario, if you look closely at the value for Model compared to Make, you'll notice it is slightly indented. There is a leading space in front of the Model values, so the SUBSTR function is extracting the space rather than the first letter each time.

 

stever_8_Example5.jpg

 

 

Also, the length of the new variable Series is determined by the first value assigned in the program. Because the first time Series appears in the program a value of A is assigned, the new variable is given a length of 1. This results in Other being truncated to the single letter O. Close the debugger and modify the code as follows:

 

data Audi;
    set sashelp.cars(keep=Make Model MSRP);
    length Series $ 5;
    where Make="Audi";
    SeriesCode=substr(left(Model),1,1);
    if SeriesCode="A" then Series="A";
    else Series="Other";
    keep Make Model Series MSRP;
run;

 

Rerun the program and confirm that the proper values are assigned.

 

stever_9_AudiOutput2.jpg

 

A good exercise would be to return to the debugger again to step through the program and observe as values are created and assigned using the correct logic.

 

Final Thoughts

If you’ve ever been confused about why your DATA step isn’t producing the expected results, the debugger is one of the best learning tools available. It shifts debugging from guesswork to observation, giving you a clear window into how SAS executes your code.

Contributors
Version history
Last update:
‎03-26-2026 11:57 AM
Updated by:

Catch up on SAS Innovate 2026

Dive into keynotes, announcements and breakthroughs on demand.

Explore Now →

SAS AI and Machine Learning Courses

The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.

Get started

Article Tags