BookmarkSubscribeRSS Feed
ZRick
Obsidian | Level 7

%macro t;

proc contents data=sashelp.class out=class_vards;run;

proc sql;

select name into: varlst seperated by ',' from class_vards;

quit;

%if "Rating" in (&varlst) %then %put "it is in the list";

%else %return;

%mend;

%t

It says:

ERROR: Required operator not found in expression: "Rating" in (&varlst)

How do I fix it?

5 REPLIES 5
MichelleHomes
Meteorite | Level 14

To use the in operator in the macro facility you need to be using SAS 9.2 or SAS 9.3 and have 2 macro options set, minoperator and mindelimiter.

This is documented in the usage note, http://support.sas.com/kb/35/591.html - Click on the Full Code tab.

cheers,

Michelle

//Contact me to learn how Metacoda software can help keep your SAS platform secure - https://www.metacoda.com
ZRick
Obsidian | Level 7

%macro t;

proc contents data=sashelp.class out=class_vards;run;

proc sql;

select name into: varlst seperated by ',' from class_vards;

quit;

%if "Rating" NOT in (&varlst) %then %put "it is NOT in the list";

%else %put "Yes, it is in the list, continue to process";

%mend;

%t

MichelleHomes
Meteorite | Level 14

An alternative could be to use the %index macro function as in the SAS documentation, http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000543562.htm


But this method would require you to write a macro do loop to pass each value to test it.

//Contact me to learn how Metacoda software can help keep your SAS platform secure - https://www.metacoda.com
Ksharp
Super User

or try this one :

%macro t;
%local var;
proc sql noprint;
select name into: var from dictionary.columns where libname='SASHELP' and memname='CLASS' and upcase(name)='SEX';
quit;
%if &var ne %then %put "it is in the list";.
%else %return;
%mend;

%t

Ksharp

Message was edited by: xia keshan Opps, missing something

Tom
Super User Tom
Super User

If you cannot get MINOPERATOR options to work for you (or just because it is easier Smiley Happy ) you can use %SYSFUNC() to use the INDEXW() function.

%macro t;

%local class_vards;

proc contents noprint data=sashelp.class out=class_vards;run;

proc sql noprint;

select name into: varlst separated by ',' from class_vards;

quit;

%if %sysfunc(indexw(%qupcase(&varlst),AGE,%str(,))) %then %put "it is in the list";

%else %return;

%mend;

%t;


Other points:

Watch out for case when searching, you might want to add select UPCASE(NAME) in the SQL code.

Note that you do NOT want the double quotes as there will not be quotes in the value of the macro variable that PROC SQL generated.

Use the NOPRINT option on PROC CONTENTS and PROC SQL to avoid cluttering your SAS output.

Using comma as the delimiter makes the string harder to use in macro logic (that is why I used Qupcase function) and normal SAS code.  Why not use a space as the delimiter instead?

I would NOT use the %RETURN macro command as it violates the principle of single entry and single exit for program blocks.



Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 3513 views
  • 0 likes
  • 4 in conversation