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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

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.

draroda
Fluorite | Level 6
Thanks.This works.
ballardw
Super User

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.

 

 

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
  • 3 replies
  • 828 views
  • 0 likes
  • 3 in conversation