DATA Step, Macro, Functions and more

Pop Quiz, Macro Syntax

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 101
Accepted Solution

Pop Quiz, Macro Syntax

Hey all,

 

Quick question, I am trying to get the following to return "yes" and I can't.  I know that I am missing something like a %sysfunc or %syseval or something like that, but just can't figure it out.   I want to be able to have &testid defined as any of the three components of &allids, and have the macro return "yes".

 

Thanks for your help!

 


%let testid = A967;
%let allids = A967 A980 A982;
%put &testid;
%put &allids;

 

%macro johntest(inTestid, inallids);

%if "&inTestid." in "&inallids." %then %do;
%put yes;
%end;
%else %do;
%put no;
%end;

%mend;

%johntest(&testid., &allids.);


Accepted Solutions
Solution
‎03-06-2017 03:48 PM
Super User
Posts: 19,770

Re: Pop Quiz, Macro Syntax

Posted in reply to mahler_ji

If it's a pop quiz, what do we get if it we get it right?

 

Look at the option MINOPERATOR and note the default setting.

 

http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#p0pbehl7wj5sl4n1o...

 

Also, this doesn't make sense.

 

 

%if "&inTestid." in "&inallids." %then %do;

Resolves to :

%if "A967." in "A967 A980 A982" %then %do;

Remember it always needs to be valid SAS code. This isn't. 

 

The following works:

 

option minoperator;
%let testid = A888;
%let allids = "A967" "A980" "A982";
%put &testid;
%put &allids;
 
%macro johntest(inTestid, inallids);
%if "&inTestid." in (&inallids.) %then %do;
%put yes;
%end;
%else %do;
%put no;
%end;
%mend;
%johntest(&testid., &allids.);

 

 

 

 

 

View solution in original post


All Replies
PROC Star
Posts: 307

Re: Pop Quiz, Macro Syntax

Posted in reply to mahler_ji

 

Minor syntax issues aside, you need the "minoperator" option.


option minoperator;
%let testid = A967;
%let allids = A967 A980 A982;
%put &testid;
%put &allids;

%macro johntest(inTestid, inallids);
%if &inTestid in (&inallids) %then %do;
%put yes;
%end;
%else %do;
%put no;
%end;
%mend;
%johntest(&testid, &allids);

Solution
‎03-06-2017 03:48 PM
Super User
Posts: 19,770

Re: Pop Quiz, Macro Syntax

Posted in reply to mahler_ji

If it's a pop quiz, what do we get if it we get it right?

 

Look at the option MINOPERATOR and note the default setting.

 

http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#p0pbehl7wj5sl4n1o...

 

Also, this doesn't make sense.

 

 

%if "&inTestid." in "&inallids." %then %do;

Resolves to :

%if "A967." in "A967 A980 A982" %then %do;

Remember it always needs to be valid SAS code. This isn't. 

 

The following works:

 

option minoperator;
%let testid = A888;
%let allids = "A967" "A980" "A982";
%put &testid;
%put &allids;
 
%macro johntest(inTestid, inallids);
%if "&inTestid." in (&inallids.) %then %do;
%put yes;
%end;
%else %do;
%put no;
%end;
%mend;
%johntest(&testid., &allids.);

 

 

 

 

 

Valued Guide
Posts: 505

Re: Pop Quiz, Macro Syntax

Posted in reply to mahler_ji
You need

options minoperator; %macro johntest(inTestid, inallids)/minoperator; SOLUTION %let testid = A967; %let allids = A967 A980 A982; %put &testid; %put &allids; options minoperator; %macro johntest(inTestid, inallids)/minoperator; %if &inTestid. in (&inallids.) %then %do; %put yes; %end; %else %do; %put no; %end; %mend; %johntest(&testid., &allids.); 554 %let testid = A967; 1555 %let allids = A967 A980 A982; 1556 %put &testid; SYMBOLGEN: Macro variable TESTID resolves to A967 A967 1557 %put &allids; SYMBOLGEN: Macro variable ALLIDS resolves to A967 A980 A982 A967 A980 A982 1558 options minoperator; 1559 %macro johntest(inTestid, inallids)/minoperator; 1560 %if &inTestid. in (&inallids.) %then %do; 1561 %put yes; 1562 %end; 1563 %else %do; 1564 %put no; 1565 %end; 1566 %mend; 1567 %johntest(&testid., &allids.); MLOGIC(JOHNTEST): Beginning execution. SYMBOLGEN: Macro variable TESTID resolves to A967 SYMBOLGEN: Macro variable ALLIDS resolves to A967 A980 A982 MLOGIC(JOHNTEST): Parameter INTESTID has value A967 MLOGIC(JOHNTEST): Parameter INALLIDS has value A967 A980 A982 SYMBOLGEN: Macro variable INTESTID resolves to A967 SYMBOLGEN: Macro variable INALLIDS resolves to A967 A980 A982 MLOGIC(JOHNTEST): %IF condition &inTestid. in (&inallids.) is TRUE MLOGIC(JOHNTEST): %PUT yes yes MLOGIC(JOHNTEST): Ending execution.
Valued Guide
Posts: 505

Re: Pop Quiz, Macro Syntax

Posted in reply to rogerjdeangelis
If testid can be null 


* fails;
%johntest(%str( ), &allids.);


This works with nulls


if testid can be  null


%let testid = "A967";
%let allids = "A967" "A980" "A982";
%put &testid;
%put &allids;


options minoperator;
%macro johntest(inTestid, inallids)/minoperator;
%if &inTestid. in (&inallids.) %then %do;
%put yes;
%end;
%else %do;
%put no;
%end;
%mend;
%johntest(%str(""), &allids.);

Frequent Contributor
Posts: 101

Re: Pop Quiz, Macro Syntax

Posted in reply to rogerjdeangelis

All,

 

Thanks very much for your replies.  One quick thing.

 

Is there a way for me to turn back off this option so that it doesn't inadvertently affect other parts of the code that it shouldn't?

PROC Star
Posts: 307

Re: Pop Quiz, Macro Syntax

Posted in reply to mahler_ji

NOMINOPERATOR

Super User
Posts: 19,770

Re: Pop Quiz, Macro Syntax

Posted in reply to mahler_ji

Option nominoperator;

Super User
Posts: 11,343

Re: Pop Quiz, Macro Syntax

Posted in reply to mahler_ji

You may not have the macro IN operator turned on and possibly have a different delimiter

See if this helps:

 

%macro johntest(inTestid, inallids)/minoperator mindelimiter=' ';
   %if %eval(&inTestid in &inallids) %then %do;
      %put yes;
   %end;
   %else %do;
      %put no;
   %end;
%mend;

%johntest(&testid, &allids);
Frequent Contributor
Posts: 101

Re: Pop Quiz, Macro Syntax

Thank you all!

 

You are the best.  I will test this out right now!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 211 views
  • 1 like
  • 5 in conversation