Search code examples
sassas-macro

How to prevent CATx functions from evaluating expression


When calling CATT() function with %sysfunc, is there a way to stop it from evaluating an expression?

For example given the code:

%let date=10-13-2015;
%put %sysfunc(catt(The date Is:,&date));

I would like it to return:

The date Is:10-13-2015

Because 10-13-2015 is just a text string. But instead CATT() sees hyphen as a subtraction sign and evaluates it as a numeric expression, returning:

The date Is:-2018

I have tried macro quoting, but doesn't change anything, I suppose because I need to somehow hide the values from CATT(). Seems if any argument to CATT looks like an expression, it will be treated as such.

Another example:

%let value=2 and 3;
%put %sysfunc(catt(The value Is:,&value));
The value Is:1

Solution

  • Provided you can do so, just remove the comma - there's no need to separate it into an individual parameter (unless you're using catx() rather than catt():

    %let date=10-13-2015;
    %put %sysfunc(catt(The date Is: &date));
    

    Personally, I think the best way to work is to store the date as a SAS date value and then use the second (optional) parameter of %sysfunc to apply the formatting. This provides better flexibility.

    %let date = %sysfunc(mdy(10,13,2015));
    %put The date Is: %sysfunc(sum(&date),mmddyyd10.);
    

    If you are insistent on the original approach and are using catx(), then I don't know how to do it exactly. The closest I could get was to insert a piece of text so it couldn't be interpreted as an expression, and then remove that text afterwards using tranwrd. Pretty, ugly, and it leaves a space:

    %let date=10-13-2015;
    %let tmp=%sysfunc(catx(#, The date Is: , UNIQUE_STRING_TO_REMOVE&date ));
    %let want=%sysfunc(tranwrd(&tmp, UNIQUE_STRING_TO_REMOVE, ));
    
    %put &want;
    

    Gives:

    The date Is:# 10-13-2015
    

    I also tried every combination of macro quoting, and scanned through the entire SAS function list and couldn't see any other viable options.