DATA Step, Macro, Functions and more

Pass strings with special characters as input to macro

Reply
Frequent Contributor
Frequent Contributor
Posts: 101

Pass strings with special characters as input to macro

[ Edited ]

 

Hi,

I am trying to run a macro with the following input and it works perfect.

%medc(pdrug=drp1|drp2|drp3|drrp4, dset=data1);

 

But, if any special characters in the input such as the following, the macro doesnt work.

 

%medc(pdrug=drp1 w/drp5|drp2|drp3 w/drp5/drp6/drp7|drp4, dset=data1);

 

Any idea to let the macro recognise these special characters?

 

Thanks

 

Super User
Posts: 9,682

Re: Pass strings with special characters as input to macro

'/' ' ' are special characters for macro device , you need mask them via %str()

 

 

%medc(pdrug=%str(drp1 w/drp5|drp2|drp3 w/drp5/drp6/drp7|drp4) , dset=data1);

Frequent Contributor
Frequent Contributor
Posts: 101

Re: Pass strings with special characters as input to macro

@Ksharp: Thanks for the quick response.

 

However, program end up with follwoing error.

ERROR: The regular expression passed to the function PRXMATCH contains a syntax error.

 

 

Super User
Posts: 10,516

Re: Pass strings with special characters as input to macro


ari wrote:

@Ksharp: Thanks for the quick response.

 

However, program end up with follwoing error.

ERROR: The regular expression passed to the function PRXMATCH contains a syntax error.

 

 


Please show the macro call you used and the macro code. The log generated with Options MPRINT may be helpful as well.

Frequent Contributor
Frequent Contributor
Posts: 101

Re: Pass strings with special characters as input to macro

%macro medc(str=,dset=,);
data &dset;
set data;
if prxmatch("m/&pdrug/oi", code)>0;
run;
%mend medc;

macro call: %medc(pdrug=drp1 w/drp5|drp2|drp3 w/drp5/drp6/drp7|drp4, dset=data1);
Super User
Posts: 9,682

Re: Pass strings with special characters as input to macro

'/' is keyword for PRX , you need add escape char '\' before it.

 

data data;
code='drp1 w/drp5';
run;


%macro medc(pdrug=,dset=,);
data &dset;
set data;
if prxmatch("m/&pdrug/oi", code)>0;
run;
%mend medc;


%medc(pdrug=drp1 w\/drp5|drp2|drp3 w\/drp5\/drp6\/drp7|drp4, dset=data1)

Super User
Super User
Posts: 7,407

Re: Pass strings with special characters as input to macro

Is there a need to pass them as a macro paramter?  Why not pass in a dataset with your list of parameters - it doesn't matter then how long your parameters are or what they contain.  If you can provide some test data (form of a datastep) and what the output should like like I can show you.

Frequent Contributor
Frequent Contributor
Posts: 101

Re: Pass strings with special characters as input to macro

I need to provide input as macro parameter to create datasets based on the match 

 

%macro medc(str=,dset=,);
data &dset;
set data;
if prxmatch("m/&pdrug/oi", code)>0;
run;
%mend medc;

Super User
Super User
Posts: 7,407

Re: Pass strings with special characters as input to macro

So, just as an example there (note, this is just a demo - it doesn't run, you need to update with your data/variables etc.):

data parameters;
  param="abc"; output;
  param="def"; output;
run;

%macro your_macro (ds=);
  proc sql;
    create table WANT as
    select   A.VARIABLES,
             B.VARIABLES
    from     BASE_DATASET A
    left join &DS. B
    on        prxmatch(B.PARAM,A.CODE);
  quit;
%mend your_macro;

%your_macro (ds=parameters);

 

 

Frequent Contributor
Frequent Contributor
Posts: 101

Re: Pass strings with special characters as input to macro

@RW9, Thanks, but what if multiple parameters need to be given as input such as in the following case
%conmd(pdrug=T|P|C|A|R, dset=ta ,flg=tac);
Super User
Super User
Posts: 7,407

Re: Pass strings with special characters as input to macro

As always there is many ways of of doing it, you could just replace the list with the dataset, you could put all parameters in the dataset etc.

%conmd(pdrug=params, dset=ta ,flg=tac);

Or, all params in a dataset:

data params:
  param="Values"; item="T"; output;
  param="Values"; item="P": output;
  param="Dset"; item="ta"; output;
  param="Flg"; item="tac"; output;
run;

%macro temp (params=);
...
%mend temp;

%temp (params=params);
Frequent Contributor
Frequent Contributor
Posts: 101

Re: Pass strings with special characters as input to macro

@RW9, this solution works but I have lot (>500) of strings to pass as input to macro, any idea to make this script work in this case??
PROC Star
Posts: 1,233

Re: Pass strings with special characters as input to macro

I don't know much of the PRX world, but it looks to me like your problem is with the regular expression, not the macro language. 

 

If you remove the macro language, can you get the regular expression to work?  It looks to me like your current macro would generate the below code, which fails with that error message.

 

39   data want;
40   set sashelp.class;
41   if prxmatch("m/drp1 w/drp5|drp2|drp3 w/drp5/drp6/drp7|drp4/oi", name)>0;
42   run;

ERROR: Invalid characters "drp5|drp2|drp3 w/drp5/drp6/drp7|drp4/oi" after end delimiter "/" of
       regular expression "m/drp1 w/drp5|drp2|drp3 w/drp5/drp6/drp7|drp4/oi".
ERROR: The regular expression passed to the function PRXMATCH contains a syntax error.

 

So maybe those blanks in your expression are a problem?


Often the first step of macro-debugging is figuring out whether it is the macro language throwing an error or the SAS language.  And it often helps to back out of the macro language so that you figure out the SAS code that works first, then figure out how to use the macro language to generate that SAS code.

 

HTH.

Ask a Question
Discussion stats
  • 12 replies
  • 412 views
  • 3 likes
  • 5 in conversation