BookmarkSubscribeRSS Feed
ari
Quartz | Level 8 ari
Quartz | Level 8

 

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

 

12 REPLIES 12
Ksharp
Super User

'/' ' ' 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);

ari
Quartz | Level 8 ari
Quartz | Level 8

@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.

 

 

ballardw
Super User

@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.

ari
Quartz | Level 8 ari
Quartz | Level 8
%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);
Ksharp
Super User

'/' 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)

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

ari
Quartz | Level 8 ari
Quartz | Level 8

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;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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);

 

 

ari
Quartz | Level 8 ari
Quartz | Level 8
@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);
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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);
ari
Quartz | Level 8 ari
Quartz | Level 8
@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??
Quentin
Super User

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.

The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 4255 views
  • 3 likes
  • 5 in conversation