BookmarkSubscribeRSS Feed
David_Billa
Rhodochrosite | Level 12

I've the macro variable _aud_infile and it resolves to IFK_GTR_TRN_1_6010_1_20210127T163229.csv. Now I want to scan the value of the macro variable to see if string contains 5601 or 6010 or 6020. If it matches then I want to do execuete some conditions.

 

But I;m getting the error as shown below although the string matches with the macro variable. Any help?

 

2407                     %if (%sysfunc(scan(&_aud_infile.,5,"_")) in ('5601','6010','6020')  %then %do;
ERROR: Macro keyword DO appears as text.
ERROR: A dummy macro will be compiled.
4 REPLIES 4
PaigeMiller
Diamond | Level 26

Make sure you have the correct number of open and close parentheses, I don't think you do. Make sure they are in the right place.

 

Inside of %SYSFUNC, you do not put arguments to functions in quotes or double-quotes.


I don't think the operator IN works in macro expressions, instead you might want to use the %INM macro function

 

%macro inm(slist,s);
    /* SAS Macro %inm to see if &s is contained in a string or list &slist                 */
    /* Borrowed from https://groups.google.com/forum/#!topic/comp.soft-sys.sas/fWcSDgg11tE */
    %if %sysfunc(indexw(&slist,&s)) gt 0 %then 1 ;
    %else 0;
%mend;

 

--
Paige Miller
maguiremq
SAS Super FREQ

Here's another solution using regular expressions. @PaigeMiller's solution is more concise and what I'd suggest given that mine may not be totally intuitive, but I think it's always helpful to see multiple solutions.

 

Also, my solution assumes there's only one macro variable you're trying to parse. You'd need to provide another parameter/modify the macro if you have multiple.

 

%let _aud_infile = IFK_GTR_TRN_1_6010_1_20210127T163229.cs;

%macro test (values);
/* If you find the pattern specified in the macro call below (prxmatch > 0), then load sashelp.cars where the make is Toyota */
%if %sysfunc(prxmatch("&values.", &_aud_infile.)) > 0 %then %do; /* notice that I have the values parameter in quotes. */
	data work.toyotas;
		set sashelp.cars (where = (make = "Toyota"));
	run;
%end;
/* If you don't find the pattern in the macro call below (prxmatch = 0), then load sashelp.cars where make isn't Toyota */
%else %do;
	data work.nontoyotas;
		set sashelp.cars (where = (make ^= "Toyota"));
	run;
%end;
%mend test;

options symbolgen; /* For debugging purposes */

/* You have to specify pipes (|) separating the values for the regular expression search */

%test(5601|6010|6020); /* Pattern exists in this set of values. Should load Toyotas. */
%test(9999|2222|2010); /* Pattern does not exist in this set of values. Should load non-Toyotas */
%test(9999|1111|6010); /* Pattern exists in this set of values. Should load Toyotas.*/

 

 

Tom
Super User Tom
Super User

Why are you using %SYSFUNC() to call the data step function SCAN() instead of just using the built in macro function %SCAN()?  Why are you including double quote characters in the list of delimiters for the scan function?  Does the filename you are scanning includes the double quotes?  Why are you inserting single quote characters into the strings you are looking for?  I doubt that you filenames contain those single quote characters around the number strings you are looking for.

%macro test(mvar) /minoperator ;
%local i found;
%let found=0;
%do i=1 %to %sysfunc(countw(&mvar,_));
  %if %scan(&mvar,&i,_) in 5601 6010 6020 %then %let found=1;
%end;
%put &=found;
%mend test;

Test:

640   %test(IFK_GTR_TRN_1_6010_1_20210127T163229.csv) ;
FOUND=1
641   %test(junk.csv) ;
FOUND=0

 

 

Kurt_Bremser
Super User

Macro.

Macro.

Macro.

 

The macro processor is a PURE TEXT PROCESSOR and sees everything as text, so no quotes are needed. No quotes are needed.

Next, for the IN to work, the corresponding system option must be set. And the documentation for that (Maxim 1!) contains several examples for the use of macro IN to arrive at this solution:

options minoperator;

%let _aud_infile = IFK_GTR_TRN_1_6010_1_20210127T163229.csv;

%if %scan(&_aud_infile.,5,_) in 5601 6010 6020
%then %do;
  %put Yes!;
%end;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 864 views
  • 1 like
  • 5 in conversation