BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
djbateman
Lapis Lazuli | Level 10

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);

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

2 REPLIES 2
Astounding
PROC Star

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.

djbateman
Lapis Lazuli | Level 10

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 9026 views
  • 1 like
  • 2 in conversation