BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Caetreviop543
Obsidian | Level 7

Hello,

 

I am trying to use the %str function to modify my code. Here it is:

 

%macro predictor;

 

data new;

set old;

pred=1/(1+exp(-1*(Intercept+ (AGE_P*AGE)+(SEX_P*SEX)+(EDU_P*EDU)

%if MRT=1 %then %str(

+ (MRT_P1))));

);

%else %if MRT=2 %then %str(

+ (MRT_P2))));

);

%else %str(

)));

);

run;

 

%mend predictor;

%predictor;

 

When I plug what's in the string functions individually, it works, so I know it doesn't have anything to do with how I coded the content (i.e. missing parentheses/semicolons). I've also used this function before in similar contexts, and never had a problem. 

 

I'm getting the dreaded "There is no matching %IF statement for the %ELSE...A dummy macro will be compiled," error.

 

Any help would be appreciated.

 

Thanks in advance!

 

Emily

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Macro language doesn't do what you are trying to make it do.  Macro language does not examine the contents of a DATA step variable in order to change the resulting SAS statements.  Macro language can only change the statements that go into a DATA step, before actually examining the data.

 

All is not lost.  You can fairly easily convert the formula to escape the need for macro language.  For example:

 

if mrt=1 then temp=mrt_p1;

else if mrt=2 then temp=mrt_p2;

else temp=0;

pred=1/(1+exp(-1*(Intercept+ (AGE_P*AGE)+(SEX_P*SEX)+(EDU_P*EDU) + (temp) )));

 

I think I got the parentheses balanced, but you will need to test to be sure.

 

If you actually have many possible values that you might want to use, arrays might be a possibility.  But macro language just isn't built to do this job.

 

 

 

 

View solution in original post

6 REPLIES 6
Astounding
PROC Star

Macro language doesn't do what you are trying to make it do.  Macro language does not examine the contents of a DATA step variable in order to change the resulting SAS statements.  Macro language can only change the statements that go into a DATA step, before actually examining the data.

 

All is not lost.  You can fairly easily convert the formula to escape the need for macro language.  For example:

 

if mrt=1 then temp=mrt_p1;

else if mrt=2 then temp=mrt_p2;

else temp=0;

pred=1/(1+exp(-1*(Intercept+ (AGE_P*AGE)+(SEX_P*SEX)+(EDU_P*EDU) + (temp) )));

 

I think I got the parentheses balanced, but you will need to test to be sure.

 

If you actually have many possible values that you might want to use, arrays might be a possibility.  But macro language just isn't built to do this job.

 

 

 

 

Caetreviop543
Obsidian | Level 7

Thanks!!

 

So you can't use macro functions when trying to modify variables in a data step? Is that correct? I've used %if %then %do statements and macro variables in data steps and not had any issues. I guess I've only used %if %then %str in a proc though.

 

Regardless, your response was super helpful! Creating the temp variable resolved my issue with differences between the predicted values produced by SAS and by hand.

 

Emily

Astounding
PROC Star

It sounds like you're getting the idea.  A good way to simplify these ideas is this.

 

Macro language helps you construct the statements that become part of the program.

 

Once the program is constructed (whether using macro language or not), after that is when the program executes.

 

You might want to review programs you have written that utilize macro language, and look at the programs in that light.

ballardw
Super User

 

In

%if MRT=1 %then %str(
+ (MRT_P1)  )   ));
);          ^

The ) indicated with the ^ matches the ( for the %str function. So you have two semicolons seen by the interpreter before the %else. So the interpreter using the first ; as the end of the %if /%then as a single instruction. then the other ); which I assume is supposed to close the Pred= assignment is encountered, the %else. So there isn't an available %if.

 

 

And why are you using %str in the first place? I suspect an %if %then %do; <code> %end;%else %do; <code> %end; %else %do;<other code> %end;  and then close the assignments ;

 

Run your code with option mprint; and see what it is actually generating when you call that macro.

Caetreviop543
Obsidian | Level 7

Ok. thanks for your response. I was using %str function because it was less code. I did eventually resort to %if %then %do %end, but I think Astounding's response is the most succinct/straightforward.

Tom
Super User Tom
Super User

Remember that to the macro processor

MRT=1

is always false since the strings are different in the first character. Plus one is three characters long and the other is only one character long.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1740 views
  • 0 likes
  • 4 in conversation