BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Davanden
Obsidian | Level 7

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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().

 

 

View solution in original post

5 REPLIES 5
Reeza
Super User
%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.

yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Davanden
Obsidian | Level 7

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.

Tom
Super User Tom
Super User

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().

 

 

Davanden
Obsidian | Level 7

Thank you for that insight.  That's a better way to do it.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 2460 views
  • 1 like
  • 4 in conversation