DATA Step, Macro, Functions and more

Macro IF-THEN Character Comparison

Accepted Solution Solved
Reply
Regular Contributor
Posts: 237
Accepted Solution

Macro IF-THEN Character Comparison

I am having a hard time getting through a macro IF-THEN statement.  I have the following code that does not seem to be working:

%if %str(&protocol.)=%str(EC-FV-04) | %str(&protocol.)=%str(EC-FV-06) %then %do;

...

%end;

I am getting the error: "A character operand was found in the %EVAL function or %IF condition where a numeric operand is required."  I am assuming that the hyphens are telling SAS to do a mathematical subtraction, which it cannot do with characters (nor do I want it to do so).  I have tried various ways to run the comparison, but I am not having much luck.

As a side note, is it possible to use the "in" operator in a macro?  I would like to be able to state:

%if %str(&protocol.) in (%str(EC-FV-04),%str(EC-FV-06)) %then %do;

...

%end;

Here is my full code, if it will help:

%macro CTSCHEDULE (protocol=%str(),drugstartdt=);

      %let drugstartdt=%sysfunc(upcase(&drugstartdt.));

      %put &drugstartdt.;

      data _null_;

            position=prxmatch("/\d\d[A-Z][A-Z][A-Z]\d{4}/","&drugstartdt.");

            call symputx('position',position);

      run;

      %if %length(&drugstartdt.)^=9 %then %do;

            %put ERROR: Incorrect Length.  DRUGSTARTDT must be in the form DDMMMYYYY.;

            %return;

      %end;

      %if &position.^=1 %then %do;

            %put ERROR: Incorrect Format.  DRUGSTARTDT must be in the form DDMMMYYYY.;

            %return;

      %end;

      data schedule;

            do CYCLE=1 to 30;

                  do CYCLEWEEK=1 to 4;

                        output;

                  end;       

            end;

      run;

      data schedule;

            set schedule;

            EVENT='C' || compress(put(cycle,best.)) || 'W' || compress(put(cycleweek,best.));

            STUDYWEEK=_n_;

            CTDT="&drugstartdt."d + (studyweek-1)*7;

            format ctdt date9.;

      run;

      data ctschedule;

            set schedule;

            %if %str(&protocol.)=%str(EC-FV-04) | %str(&protocol.)=%str(EC-FV-06) %then %do;

                  if studyweek<=24 & mod(studyweek,6)=0 then CT=1;

                        else if studyweek>24 & mod(studyweek,8)=0 then CT=1;

            %end;

            %if %str(&protocol.)=%str(EC-FV-07) %then %do;

                  if mod(studyweek,6)=0 then CT=1;

            %end;

            MINDT=ctdt-4;

            MAXDT=ctdt+4;

            format mindt maxdt date9.;

            if CT=1;

            drop CT;

      run;

%mend CTSCHEDULE;

%ctschedule(protocol=%str(EC-FV-04),drugstartdt=06JAN2014);


Accepted Solutions
Solution
‎09-11-2014 10:11 AM
Super User
Posts: 5,518

Re: Macro IF-THEN Character Comparison

Posted in reply to djbateman

First, let's deal with the source of the error.  Yes, the hyphens are causing a problem, but  you have correctly eliminated half of the problem.  The %STR function takes care of the hard-coded hyphens.  The problem is that there are no hyphens appearing in &PROTOCOL. when the macro is defined.  There is nothing to quote until later, when the macro actually executes and replaces &PROTOCOL.  By that time, %STR is out of the picture.  You can overcome this by switching from %STR (which operates when the macro is defined) to %BQUOTE (which operates when the macro executes) in the offending spot:

%if %bquote(&protocol.) = %str( ...

An easier solution might be to add double quotes.  This forces macro language to treat each string as character, suppressing any attempt to perform math.

%if "&protocol." = "EC-FV-04" ...

It's not that macro language needs qutoes to compare character strings ... it doesn't.  But the quotes suppress the automatic attempt to execute arithmetic operators.

Finally, there is an IN operator in macro language (depends on the release of SAS as to whether it exists, and whether it works properly).  Take a look at documentation/examples for the MINOPERATOR option.

Good luck.

View solution in original post


All Replies
Solution
‎09-11-2014 10:11 AM
Super User
Posts: 5,518

Re: Macro IF-THEN Character Comparison

Posted in reply to djbateman

First, let's deal with the source of the error.  Yes, the hyphens are causing a problem, but  you have correctly eliminated half of the problem.  The %STR function takes care of the hard-coded hyphens.  The problem is that there are no hyphens appearing in &PROTOCOL. when the macro is defined.  There is nothing to quote until later, when the macro actually executes and replaces &PROTOCOL.  By that time, %STR is out of the picture.  You can overcome this by switching from %STR (which operates when the macro is defined) to %BQUOTE (which operates when the macro executes) in the offending spot:

%if %bquote(&protocol.) = %str( ...

An easier solution might be to add double quotes.  This forces macro language to treat each string as character, suppressing any attempt to perform math.

%if "&protocol." = "EC-FV-04" ...

It's not that macro language needs qutoes to compare character strings ... it doesn't.  But the quotes suppress the automatic attempt to execute arithmetic operators.

Finally, there is an IN operator in macro language (depends on the release of SAS as to whether it exists, and whether it works properly).  Take a look at documentation/examples for the MINOPERATOR option.

Good luck.

Regular Contributor
Posts: 237

Re: Macro IF-THEN Character Comparison

Posted in reply to Astounding

Thank you so much!  I was looking at the wrong side of the "=" sign.  Thank you for explaining the &PROTOCOL portion.  I was also able to get the IN operator to work with MINOPERATOR.  Thanks again.

🔒 This topic is solved and locked.

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

Discussion stats
  • 2 replies
  • 362 views
  • 0 likes
  • 2 in conversation