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

Hi everyone,

I am having some trouble with a seemingly simple problem.

I have a series of macro variables, with names Variable1, Variable2, Variable3, etc.

I am trying to create a new macro variable that is a concatenation of all of these individual macro variables. So, let's say we have:

%let Variable1 = freq;

%let Variable2 = seq;

%let Variable3 = abc;

Then I want a new variable:

NewVar = freq seq abc;

However, I need my code to be generalized, because different times I run this code I will have varying numbers of the individual macro variables. How do I go about doing this?

I have tried this:

%let k=3;

%let x=variable;

%let str=;

%macro test;

     %do i=1 %to &k;

     %let str=%sysfunc(catx(str,&&&x.&i));

     %put str;

%mend;

%test;

However, this doesn't work. This will just overwrite the "str" macro variable instead of iteratively concatenating it.

How do I go about iteratively concatenating macro variables?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Your code has several syntax errors, but the general logic is correct.

Syntax errors:

1. Missing % in front of TO in %DO loop

2. No %END for %DO loop

3. CATX function is not used properly - first argument is the delimiter

4. CATX function refers to STR instead of &STR to resolve the macro variable

5. It's %sysfunc not &sysfunc

6. To resolve the macro variables to the values you'll need some extra ampersands.

Find the correct CAT function that will work for you to provide spaces between the words, otherwise here's a version that partially works:

%let k=3;

%let x=variable;

%let str=;

%macro test;

     %do i=1 %to &k;

     %let str=%sysfunc(catx(-, &str,&&&x.&i));

     %put &str;

  %END;

%mend;

%test;

View solution in original post

6 REPLIES 6
ballardw
Super User

The first argument to the CATX function should be a character you want to separate the variables

Plus you have a few macro syntax errors, %to instead of 'to' in the do loop, missing an %end for the do loop,

should be %sysfunc not &   (hint: % is functions, & is variables) but you want. And in your example the &&&x

But see if this gives a better idea of where to start.

%macro test;

     %do i=1 %to &k;

     %let str=&str &x;   /* yes this tacks on the save value 3 times, but with out test values for whatever may have been intended for &&&X.&i ..., */

     %put &str;

     %end;

%mend;

RyanSimmons
Pyrite | Level 9

Ah, sorry about those mistakes. I was typing that up in a hurry; I didn't have those mistakes in my actual SAS code.

Also, not sure what you mean by "with out test values for whatever may have been intended for &&&X.&i .", since in my post I did specifically give test values for what I wanted.

In any case, this does work. For some reason I didn't realize that you could concatenate macro variables without using an explicit CAT function. Thanks!

ballardw
Super User

You gave values for one variable X

%let x=variable;

%let i=1;

%put &&&x.&i;

Tries to find the resolved value for &variable1 which you did not provide, an may not have been what you want. You may have meant &&x.&i which resolves to the text Variable1.

Reeza
Super User

@ballardw It's in the post:

I am trying to create a new macro variable that is a concatenation of all of these individual macro variables. So, let's say we have:

%let Variable1 = freq;

%let Variable2 = seq;

%let Variable3 = abc;

Then I want a new variable:

NewVar = freq seq abc;

Reeza
Super User

Your code has several syntax errors, but the general logic is correct.

Syntax errors:

1. Missing % in front of TO in %DO loop

2. No %END for %DO loop

3. CATX function is not used properly - first argument is the delimiter

4. CATX function refers to STR instead of &STR to resolve the macro variable

5. It's %sysfunc not &sysfunc

6. To resolve the macro variables to the values you'll need some extra ampersands.

Find the correct CAT function that will work for you to provide spaces between the words, otherwise here's a version that partially works:

%let k=3;

%let x=variable;

%let str=;

%macro test;

     %do i=1 %to &k;

     %let str=%sysfunc(catx(-, &str,&&&x.&i));

     %put &str;

  %END;

%mend;

%test;

Tom
Super User Tom
Super User

You do not need to use CAT() functions to concatenate macro variable values. Just reference the variables next to each other and they are concatenated. (For example: &a.&b)

You showed an example using CATX() which adds a separator, So here is a version in that spirit. If you do not need the separator then do not provide one.

%let Variable1 = freq;

%let Variable2 = seq;

%let Variable3 = abc;

%let result= ;

%macro test ;

  %local i sep ;

  %do i=1 %to 3 ;

    %let result=&result.&sep.&&variable&i;

    %let sep=,;

  %end;

%mend test;

%test;

%put result=&result ;



result=freq,seq,abc



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
  • 6 replies
  • 10513 views
  • 1 like
  • 4 in conversation