BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
djbateman
Lapis Lazuli | Level 10

I am trying to concatenate a string of sequential numbers when the number of numbers is variable.  I have tried this little snipet below.  It should display "1,2,3" but instead displays "of pos1-pos3".  Can anyone see what is going on?

%macro simple(numpos=);

      %do i=1 %to &numpos.;

            %let pos&i.=&i.;

      %end;

      %let pos=%sysfunc(catx(",",of pos1-pos&numpos.));

      %put &pos.;

%mend simple;

%simple(numpos=3);

Once this is solved, I want to make sure that the string "1,2,3" can treat the values as numeric values so I can use it in something like:

data XXX;

      set YYY;

      where var in (&pos.);

run;

Will this be possible?

1 ACCEPTED SOLUTION

Accepted Solutions
jwsquillace
SAS Employee

FrankPoppe is correct, the OF construct does not work for macro language.  I also have not found the CATX function (or CATS, etc) to be terribly useful in macros since native macro language has good text handling features.

Here is another possible solution:

%macro simple(numpos=);
%do i=1 %to &numpos.;
  %let pos&i.=&i.;
%end;
%let pos=;  /* if macro variable needs to be cleared */
%do i = 1 %to &numpos;
  %if &i = 1 %then
    %let pos = &pos1;
  %else
    %let pos = &pos,&&pos&i;
%end;
%put &pos.;
%mend simple;

%simple(numpos=3);

Jan

View solution in original post

4 REPLIES 4
djbateman
Lapis Lazuli | Level 10

I found a little work-around on my problem.  I can simply do this:

%macro simple(numpos=);

data XXX;

      set YYY;

      where var in (%do set=1 %to &numpos.-1;   &set.,      %end; &numpos.);

run;

%mend simple;

%simple(numpos=3);

But I would still be interested in knowing why the %sysfunc above doesn't display what I would expect.

FrankPoppe
Quartz | Level 8

Hi,

The OF construct is SAS Language, not macro language, i.e. something you can use in the data step, not within a macro.

It would be possible to have the macro generate data step code that uses the OF construct, but that is not what you are doing here.

data_null__
Jade | Level 19

You don't need to use commas for regular SAS IN operator.  Probably required for IN in SQL.

jwsquillace
SAS Employee

FrankPoppe is correct, the OF construct does not work for macro language.  I also have not found the CATX function (or CATS, etc) to be terribly useful in macros since native macro language has good text handling features.

Here is another possible solution:

%macro simple(numpos=);
%do i=1 %to &numpos.;
  %let pos&i.=&i.;
%end;
%let pos=;  /* if macro variable needs to be cleared */
%do i = 1 %to &numpos;
  %if &i = 1 %then
    %let pos = &pos1;
  %else
    %let pos = &pos,&&pos&i;
%end;
%put &pos.;
%mend simple;

%simple(numpos=3);

Jan

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 14184 views
  • 6 likes
  • 4 in conversation