DATA Step, Macro, Functions and more

CATX in %SYSFUNC

Accepted Solution Solved
Reply
Regular Contributor
Posts: 237
Accepted Solution

CATX in %SYSFUNC

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?


Accepted Solutions
Solution
‎03-21-2013 09:45 AM
SAS Employee
Posts: 26

Re: CATX in %SYSFUNC

Posted in reply to djbateman

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


All Replies
Regular Contributor
Posts: 237

Re: CATX in %SYSFUNC

Posted in reply to djbateman

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.

Contributor
Posts: 41

Re: CATX in %SYSFUNC

Posted in reply to djbateman

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.

Respected Advisor
Posts: 3,799

Re: CATX in %SYSFUNC

Posted in reply to djbateman

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

Solution
‎03-21-2013 09:45 AM
SAS Employee
Posts: 26

Re: CATX in %SYSFUNC

Posted in reply to djbateman

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

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 2792 views
  • 3 likes
  • 4 in conversation