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.

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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