DATA Step, Macro, Functions and more

Iteratively concatenate macro variables in a loop

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 98
Accepted Solution

Iteratively concatenate macro variables in a loop

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?


Accepted Solutions
Solution
‎08-17-2015 01:48 PM
Super User
Posts: 17,819

Re: Iteratively concatenate macro variables in a loop

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


All Replies
Super User
Posts: 10,497

Re: Iteratively concatenate macro variables in a loop

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;

Frequent Contributor
Posts: 98

Re: Iteratively concatenate macro variables in a loop

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!

Super User
Posts: 10,497

Re: Iteratively concatenate macro variables in a loop

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.

Super User
Posts: 17,819

Re: Iteratively concatenate macro variables in a loop

@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;

Solution
‎08-17-2015 01:48 PM
Super User
Posts: 17,819

Re: Iteratively concatenate macro variables in a loop

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;

Super User
Super User
Posts: 6,499

Re: Iteratively concatenate macro variables in a loop

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



☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 995 views
  • 0 likes
  • 4 in conversation