DATA Step, Macro, Functions and more

Macro not working as expected

Accepted Solution Solved
Reply
Contributor
Posts: 31
Accepted Solution

Macro not working as expected

Hi All,

 

Can anyone please help me on below code.

 

%macro check (sdt , edt ,dif);
%if &sdt ne . and &edt ne . %then %do;
&dif=&edt - &sdt;
%end;
%if &sdt ne . and &edt eq . %then %do;
&dif= &sdt;
%end;
%if &sdt eq . and &edt ne . %then %do;
&dif=&edt ;
%end;
%mend;

data x;
infile datalines missover;
input a b ;
datalines;
10200 10300
10300 .
. 10200
;
options mlogic;
data y;
set x;
%check(a , b ,new);
run;

 

Issue i am getting with this code is that only first condition is getting true while as per data provided and macro, all three conditions should get true for either records.

 

I am pasting below snippet from log.

 


65 ;
66 options mlogic;
67 data y;
68 set x;
69 %check(a , b ,new);
MLOGIC(CHECK): Beginning execution.
MLOGIC(CHECK): Parameter SDT has value a
MLOGIC(CHECK): Parameter EDT has value b
MLOGIC(CHECK): Parameter DIF has value new
MLOGIC(CHECK): %IF condition &sdt ne . and &edt ne . is TRUE
MLOGIC(CHECK): %IF condition &sdt ne . and &edt eq . is FALSE
MLOGIC(CHECK): %IF condition &sdt eq . and &edt ne . is FALSE
MLOGIC(CHECK): Ending execution.
70 run;

 

Please help me to modify the macro in a way that all three condtions will be resolved.

 

Regards,

Rajesh


Accepted Solutions
Solution
‎02-08-2017 11:43 AM
Super User
Posts: 7,809

Re: Macro not working as expected

ALWAYS keepi in mind that the macro processor is a preprocessor that does its work before the data step is even compiled. It cannot access data step variables, it can only create text to be included as code in the data step.

So this might work:

%macro check (sdt , edt ,dif);
if &sdt ne . and &edt ne . then do;
  &dif=&edt - &sdt;
end;
if &sdt ne . and &edt eq . then do;
  &dif= &sdt;
end;
if &sdt eq . and &edt ne . then do;
  &dif=&edt ;
end;
%mend;

The macro now creates data step conditions and actions, only the variable names are taken from the macro parameters.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎02-08-2017 11:43 AM
Super User
Posts: 7,809

Re: Macro not working as expected

ALWAYS keepi in mind that the macro processor is a preprocessor that does its work before the data step is even compiled. It cannot access data step variables, it can only create text to be included as code in the data step.

So this might work:

%macro check (sdt , edt ,dif);
if &sdt ne . and &edt ne . then do;
  &dif=&edt - &sdt;
end;
if &sdt ne . and &edt eq . then do;
  &dif= &sdt;
end;
if &sdt eq . and &edt ne . then do;
  &dif=&edt ;
end;
%mend;

The macro now creates data step conditions and actions, only the variable names are taken from the macro parameters.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 31

Re: Macro not working as expected

Posted in reply to KurtBremser
Thanks.This works.
Super User
Posts: 11,343

Re: Macro not working as expected

First explain what the code is supposed to do.

Since you are placing the macro code inside a data step then show us what the generated data step should look like if the macro ran as expected.

 

 

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 127 views
  • 0 likes
  • 3 in conversation