Help using Base SAS procedures

condition on a macro-variable

Reply
New Contributor
Posts: 3

condition on a macro-variable

Hi everyone,

I have an issue with a macro-variable. I have set up a macro in which I collect values of two macro variable (call symput). But when I try to write a condition on the value of one of these macro variable, I have an error message which tells me :

ERROR: A character operand was found IN the %EVAL FUNCTION OR %IF condition WHERE a numeric

operand IS required. The condition was: &&PARCOURS_&j.="IND"

ERROR: The macro TAPIS will stop executing

Here is my macro:

%macro tapis;

data _null_; set tapis;

  CALL SYMPUT(COMPRESS("PARCOURS_"||_N_),PJC);

run;

data _null_; set tapis;

  CALL SYMPUT("Total",_n_);

run;

data tapis2; set tapis;

  %do j=1 %to &TOTAL.;

  %put PARCOURS_&j. : &&PARCOURS_&j.;

  %if &&PARCOURS_&j.="IND" %then %do;

  %put toto;

  %end;

       %end;

run;

%mend;

          %tapis;

Can you help me? Thank you in advance

New Contributor
Posts: 3

Re: condition on a macro-variable

Oh, my code doesn't appear clearly. Here it is again:

%macro tapis;

data _null_; set tapis;

  CALL SYMPUT(COMPRESS("PARCOURS_"||_N_),PJC);

run;

data _null_; set tapis;

  CALL SYMPUT("Total",_n_);

run;

data tapis2; set tapis;

  %do j=1 %to &TOTAL.;

  %put PARCOURS_&j. : &&PARCOURS_&j.;

  %if &&PARCOURS_&j.="IND" %then %do;

  %put toto;

  %end;

  %end;

run;

%mend;

%tapis;

Super Contributor
Posts: 282

Re: condition on a macro-variable

Hi,

Well done on posting your code and the error message; a sample of data can also be helpful.

To try and address the error message you are getting can you post what the log shows for:

%put PARCOURS_&j. : &&PARCOURS_&j.;

just before the error happens.

I think it would be helpful for you to use obtain more information about the macro code when it is running by submitting the following:

options mprint symbolgen mlogic;

before you run your code, as it will provide more information in the log which could be helpful if posted here.

There are some other points of note about your code such as the macro do loop *not* needing to be in a data step, and the quotes around "IND" possibly not being necessary, but I doubt they will be causing the error you've reported.

For now, please supply what the %put statement shows at the time of the error and / or the log when you use the options statement as that would be provide more information. Some sample data might also be useful.

Regards,

Amir.

PROC Star
Posts: 1,324

Re: condition on a macro-variable

Hi,

%IF statement will throw that error if you end up with a value like AND or OR which it thinks as an operator, rather than just a text value.

I added a data generation step below.  Your original code works until I add pjc="And".

To fix it, I added macro quoting (see %BQUOTE on %IF statement), which will mask the meaning of AND, OR, etc, so that they are treated as a text string.

HTH,

--Q.

%macro tapis;

%*Value AND below causes %eval to error from %IF statement ;
%*Unless use macro quoting;
data tapis;
  pjc="AAA"; output;
  pjc="IND"; output;
  pjc="And"; output; 
run;


data _null_; set tapis;
  CALL SYMPUT(COMPRESS("PARCOURS_"||_N_),PJC);
run;

 
 
data _null_; set tapis;
  CALL SYMPUT("Total",_n_);
run;

 
data tapis2; set tapis;
  %do j=1 %to &TOTAL.;
    %put PARCOURS_&j. : &&PARCOURS_&j.;

    /*%if &&PARCOURS_&j.="IND" %then %do;*/
    %if %bquote(&&PARCOURS_&j.)=IND %then %do;
      %put toto;
    %end;
  %end;
run;

%mend;

%tapis
Super User
Posts: 5,516

Re: condition on a macro-variable

Quentin has hit on the right issue.  But there is more than one possible approach.

Note that your macro variable contains the three characters IND.  It does not contain the five characters "IND".  So Quentin is right to remove the quotes when applying %BQUOTE.  However, you could also go the opposite route to make your program work.

First, switch to CALL SYMPUTX to automatically remove leading or trailing blanks:

call symputx(..., PJC);

Then use double quotes on both sides of the comparison:

%if "&&PARCOURS_&j."="IND" %then %do;

That's perhaps a simpler approach to solving the problem.

Good luck.

New Contributor
Posts: 3

Re: condition on a macro-variable

Posted in reply to Astounding

Thank you all very much for helping me. It works when I write %BQUOTE! Thanks againSmiley Happy

Ask a Question
Discussion stats
  • 5 replies
  • 427 views
  • 3 likes
  • 4 in conversation