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?
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
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.
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.
You don't need to use commas for regular SAS IN operator. Probably required for IN in SQL.
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.