SAS VIYA Guide

Beyond Macros: Data-Driven Programming with CAS in SAS Viya

Simon 25/01/2022 11 vistas

With the adoption of SAS© Viya, processing acceleration in Cloud Analytic Services (CAS) is becoming the norm. However, for seasoned SAS© programmers accustomed to the Macro language, the interaction between traditional SAS© code, the Macro language, and CASL (the native language of CAS) can seem intimidating

This article demystifies these interactions and explores how to use CASL variables for dynamic, data-driven programming, which is often more efficient than traditional macros.

1. Understanding the Architecture: Where Does Your Code Run?

It is crucial to understand that the macro processor lives only on the SAS© Compute Server. CAS does not have a macro processor

When you submit code:

  1. The Compute Server's "Word Scanner" analyzes the code.

  2. The macro processor resolves all macro references (variables & and logic %).

  3. The resulting code (properly cleaned) is sent to CAS for execution via the LIBNAME engine (Implicit Pass-Through) or via PROC CAS.

Table 1: Compute Server vs. CAS Server

CharacteristicSAS© Compute ServerCAS Server (Cloud Analytic Services)
RoleOrchestration, SAS© compilation, Macro execution.Massively parallel execution, in-memory processing.
Native LanguageSAS© (DATA Step, PROC SQL), Macro.CASL (CAS Language).
Macro ManagementYES. Resolves macros before execution.NO. Receives only the resolved code (plain text).
Data AccessSAS©/ACCESS engines (to Oracle, SQL, etc.).Pre-loaded tables in distributed memory.

Illustration

2. CASL Variables: More Powerful Than Macro Variables

Unlike macro variables, which are just text, CASL uses dynamically typed variables (the type is defined upon assignment)5. The true power of CASL lies in its composite data types.

Table 2: CASL Data Types

Data TypeDescriptionExample
Double / Int64Numbers (floating-point or exact integers).

x = 3; or y = 10.5; 

StringCharacter string (UTF-8).

name = "Mark"; 7

ArrayOrdered list of values, accessible by position. Delimited by {}.

x = {10, "Mark", "555-1212"}; 

DictionaryUnordered list of key-value pairs.

emp = {id=10, name="Mark"}; 

Result TableTwo-dimensional structure (rows/columns), often the result of a CAS Action.

Resembles an array of dictionaries.

Important Note: Arrays in CASL start at index 1 (like in SAS©), but use curly braces {} for definition and can contain mixed data types

Illustration

3. Data-Driven Programming: The DO OVER Loop

The modern and high-performance equivalent of macro loops (%DO %WHILE or loops on call execute) is the DO OVER loop in CASL. It allows for easy iteration over arrays, dictionaries, or result tables.

DO OVER Loop Syntax

  • For a Dictionary: DO , OVER ;

  • For an Array: DO OVER ;

  • For a Table: You iterate over the rows as if it were an array.

Table 3: Macro vs. CASL Comparison for Data-Driven Programming

CriterionTraditional SAS© Macro ApproachCASL Approach (Data-Driven)
MechanismGeneration of repetitive SAS©/SQL code via call execute or %do.Native iteration over in-memory objects (lists, results).
ComplexityHigh. Requires managing quotes, resolutions (&&var&i) and timing.Low. Clean and direct syntax (DO ... OVER).
PerformanceSlower (text generation + repeated compilation).

Fast (direct execution in CAS without re-parsing).

FlexibilityLimited to character strings.Manipulates numbers, strings, and complex structures.
Illustration

4. User-Defined Functions (UDFs)

Instead of writing a macro (%macro myTask; ... %mend;), you can create your own functions in CASL.

  • Advantage: The code is more concise and does not require the CAS engine to generate CASL code with each call, which is more efficient.

  • Persistence: By default, a CASL function only lasts for the duration of the PROC CAS procedure. To reuse them, you must save the code in a .sas© file and load it with readpath() and execute() at the beginning of your session.

Example of UDF logic: You can create a loadAllFiles(caslib) function that retrieves the list of files from a caslib and loops over them to automatically load them into memory, thus replacing dozens of lines of complex macro code.

Mastering the interaction between SAS© Macro and CASL is the key to becoming a "SAS© Jedi" on Viya. While macros still handle code generation on the compute server, CASL takes over to manipulate data and complex logic directly in the in-memory engine, offering speed and code elegance.