The arrival of SAS© Viya™ and the CAS (Cloud Analytic Services) engine introduced the use of FedSQL, a standardized implementation of the SQL language designed for scalability. However, FedSQL is much stricter than the classic SAS© Base PROC SQL, especially regarding data types.
A common error occurs when filtering dates using numeric macro variables.
The Problem: Strong Typing vs. Weak Typing
In classic SAS© (Base SAS©), a date is nothing more than a number (the number of days since January 1, 1960). You can therefore write without any issue:
WHERE my_date_column > 23040.
In FedSQL, types are strict. A column defined as DATE cannot be directly compared to an INTEGER. If you try to inject a raw numeric macro variable into your CAS query, you will get the following error:
ERROR: Operator is not unique: DATE > INTEGER
This means that the engine does not know how to compare these two incompatible data types without explicit conversion.
The Solution: The ANSI Format
FedSQL adheres to the ANSI SQL standard. To filter on a date, you must provide a literal value in the specific form:
DATE 'YYYY-MM-DD'
The technical challenge is to transform your numeric macro variable (e.g., 23040) into a formatted character string (e.g., '2023-01-30'), while managing the constraints of the SAS© macro language (quotes, hyphens).
The Technical Recipe
To build this string dynamically, we need to combine three tools:
PUTN: To convert the raw number into a date format with hyphens (yymmddd10.).%QSYSFUNC: To executePUTNwhile masking the hyphens (-). Without theQ(Quoting), SAS© might interpret the hyphens in the date (2023**-01-**30) as minus signs (subtraction).%TSLIT: A macro provided by SAS© that solves the problem of quotes. In SQL, literals must be enclosed in single quotes ('value'), but SAS© macros do not resolve inside single quotes.%TSLIT(Time Share Literal) generates a string enclosed in single quotes while allowing the macro inside to be resolved.
The Corrected Code
Here is how to assemble these elements for a robust FedSQL query:
