I want to build up a space-delimited from macro variables. Something like this:
%macro doesntwork;
%let first = thing1;
%let second=thing2:
%let result=%SYSFUNC(CATX(" ",&first.,&second.));
%mend;
What I want is:
thing1 thing2
The above doesn't work because it gives me thing1" "thing2. Putting a naked space character as the delimiter causes an error. Using %str( ) also causes an error.
How do I do this?
It is just doing what you asked it to do. You asked it to place the three character string " " between thing1 and thing2. If you do want the macro language to treat a space in a special way just use one of the macro quoting functions. Like %STR().
Note that there is no need to use ANY of the CAT series of data step functions with macro variables. Macro variables just contain text, so to concatenate the values of the macro variables just expand them next to each other. No need to worry about a lot of the issues caused by the floating point numbers and fixed length character strings of data step variables that functions like CATS() and CATT() were designed to deal with.
As to your specific attempt to use the CATX() function add a space between the values when both values are not missing or not add the a space when either is missing then just do:
%let first =thing1;
%let second=thing2:
%let result=&first &second;
If you do want the macro language to treat a space in a special way just use one of the macro quoting functions. Like %STR().
%let result=&first. &second.;
Usually when you want macro variable lists its easier to use SQL to create them....but it depends on overall what you're doing.
Hi,
You can do it like that:
%macro doeswork;
%let first = thing1;
%let second = thing2;
%let result = %SYSFUNC(CATX(%str( ),&first.,&second.));
%put *&result.*;
%mend;
%doeswork
but to be 100% clear I agree with @Reeza's opinion.
Bart
Thank you. When one of the macro variables is empty (which will happen in the larger program), I get this error message:
ERROR: %SYSEVALF function has no expression to evaluate.
However, it still works.
It is just doing what you asked it to do. You asked it to place the three character string " " between thing1 and thing2. If you do want the macro language to treat a space in a special way just use one of the macro quoting functions. Like %STR().
Note that there is no need to use ANY of the CAT series of data step functions with macro variables. Macro variables just contain text, so to concatenate the values of the macro variables just expand them next to each other. No need to worry about a lot of the issues caused by the floating point numbers and fixed length character strings of data step variables that functions like CATS() and CATT() were designed to deal with.
As to your specific attempt to use the CATX() function add a space between the values when both values are not missing or not add the a space when either is missing then just do:
%let first =thing1;
%let second=thing2:
%let result=&first &second;
If you do want the macro language to treat a space in a special way just use one of the macro quoting functions. Like %STR().
Thank you for that insight. That's a better way to do it.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.