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
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.