SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
egoldstein
Calcite | Level 5

I have code of the following form:

%macro y(y);

else if year=200&y. then do;

    <code>; end;

%mend y;

%macro go(com);

data &com._1

merge &com._2 &com._3 ;

by type;

if year=2001 then do;

    <code>; end;

%y(2); %y(3); %y(4); %y(5);

run;

%mend go;

%go(ABC);

When run, I get an error message on the invocation of %y(3), %y(4), and %y(5) that:

ERROR 160-185: No matching IF-THEN clause

If I change the code of the "y" macro to  just be an if statement, the problem goes away, but I can't figure out why the problem exists in the first place.  Any thoughts on why this is happening?

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

MPRINT is a good recommendation.

My guess is you have two many extra semicolons.  You dont need a semicolon after a macro call.

Try changing:

%y(2); %y(3); %y(4); %y(5);

To

%y(2) %y(3) %y(4) %y(5)

Below code shows the same errors caused by extra semicolon creating a null statement.  It breaks the else if:

options mprint;
%macro y(year);
  else if year=&year then x=1;
%mend;

data _null_;
  year=1990;

  *extra semicolons after macro call cause null statement which breaks else if error;
  if year=1 then x=1;
  %y(2000);
  %y(2001);

  *can see same errors without macro, caused by extra semicolon;
  if year=1 then x=1;
  else if year=2000 then x=1;;
  else if year=2001 then x=1;;
run;


The Boston Area SAS Users Group is hosting free webinars!
Next up: Troy Martin Hughes presents Calling Open-Source Python Functions within SAS PROC FCMP: A Google Maps API Geocoding Adventure on Wednesday April 23.
Register now at https://www.basug.org/events.

View solution in original post

3 REPLIES 3
ballardw
Super User

Use OPTIONS MPRINT ; and then run you %go macro to see what the generated code looks like.

You might also try removing the "." ad 200&y. or other places in your <code> if any.

Instead of a macro like %y I would probably do something like

Else do year= 2002 to 2005;

<code>

End;

unless code is using the &y values somewhere.

Quentin
Super User

MPRINT is a good recommendation.

My guess is you have two many extra semicolons.  You dont need a semicolon after a macro call.

Try changing:

%y(2); %y(3); %y(4); %y(5);

To

%y(2) %y(3) %y(4) %y(5)

Below code shows the same errors caused by extra semicolon creating a null statement.  It breaks the else if:

options mprint;
%macro y(year);
  else if year=&year then x=1;
%mend;

data _null_;
  year=1990;

  *extra semicolons after macro call cause null statement which breaks else if error;
  if year=1 then x=1;
  %y(2000);
  %y(2001);

  *can see same errors without macro, caused by extra semicolon;
  if year=1 then x=1;
  else if year=2000 then x=1;;
  else if year=2001 then x=1;;
run;


The Boston Area SAS Users Group is hosting free webinars!
Next up: Troy Martin Hughes presents Calling Open-Source Python Functions within SAS PROC FCMP: A Google Maps API Geocoding Adventure on Wednesday April 23.
Register now at https://www.basug.org/events.
egoldstein
Calcite | Level 5

Thanks Quentin and Ballardw!

I didn't do the do loop because I have variable names based on the value of y.

It ended up being the extra semicolons after calling the y macro.

I never knew that having extras of them could be a problem.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 4418 views
  • 3 likes
  • 3 in conversation