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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 12076 views
  • 6 likes
  • 4 in conversation