BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

I want to concatenate two macro variables with "/" between them.

I get error 49


%let customer_ID=9872622;
%let branch_Id=987;
%let customer_branch=%sysfunc(cat(&customer_ID.,"/",&branch_Id.));/***9872622/987*****/
%put &customer_branch;

 

3 REPLIES 3
Astounding
PROC Star

You are overcomplicating things.  This would be the usual way to concatenate strings in macro language:

%let customer_branch=&customer_ID./&branch_Id.;

Simple.  Macro language does not perform math in a %let statement  However, if you use the variable later, macro language may try to perform math.  For example, this statement would have the potential to cause trouble:

%if 9872622/987 = &customer_branch %then %do;

%IF/%THEN statements do try to perform math.  While there are a few ways to suppress this, an easy way would be to add double quotes:

%if "9872622/987" = "&customer_branch" %then %do;

The quotes become part of the comparison.

PaigeMiller
Diamond | Level 26

No quotes used inside %SYSFUNC

 

%let customer_branch=%sysfunc(cat(&customer_ID.,/,&branch_Id.));

 

 

But really @Astounding has the better advice.

 

Is it mandatory to use a slash here (which sometimes will be interpreted as division)? Couldn't some other character, which isn't also a mathematical operator, such as underscore, work here? Do you even need a character like slash or underscore, could you just append customer_id and branch_id without any character between the two? All of this would make your programming simpler.

--
Paige Miller
Tom
Super User Tom
Super User

I have never found any use for the CAT() series of functions in MACRO code.

 

First thing is they are just not needed.  To place values next to each other all you have to do is place the values next to each other.

%let customer_branch=&customer_ID./&branch_Id.;

Second thing is that the CAT...() functions are some the most problematic for the %SYSFUNC() macro function.  They can take either character or numeric values for all of their arguments.  So poor %SYSFUNC() has to try and GUESS whether it should tell CAT... that the values it is send are numbers or strings.

 

The only one of them that might be useful to have in macro code is CATX() since replicating the inclusion of the delimiter only when there are non-empty values would be difficult without macro logic.  So here is a SAS macro that can do for macro code what CATX() does for SAS code.

https://github.com/sasutils/macros/blob/master/catx.sas

 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 554 views
  • 1 like
  • 4 in conversation