DATA Step, Macro, Functions and more

%if %then %else %str macro functions

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

%if %then %else %str macro functions

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


Accepted Solutions
Solution
‎02-14-2018 02:11 PM
Super User
Posts: 6,637

Re: %if %then %else %str macro functions

Posted in reply to epstewart1110

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


All Replies
Solution
‎02-14-2018 02:11 PM
Super User
Posts: 6,637

Re: %if %then %else %str macro functions

Posted in reply to epstewart1110

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.

 

 

 

 

Contributor
Posts: 22

Re: %if %then %else %str macro functions

Posted in reply to Astounding

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

Super User
Posts: 6,637

Re: %if %then %else %str macro functions

Posted in reply to epstewart1110

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.

Super User
Posts: 13,338

Re: %if %then %else %str macro functions

Posted in reply to epstewart1110

 

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.

Contributor
Posts: 22

Re: %if %then %else %str macro functions

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.

Super User
Super User
Posts: 7,936

Re: %if %then %else %str macro functions

Posted in reply to epstewart1110

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.

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 101 views
  • 0 likes
  • 4 in conversation