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

I am writing a macro to create a 2x2 table and calculate sensitivity/specificity/ etc. A part of the macro is not working well, and I have no idea why. The code does not have any error message, but seems it only import the set &data, but not do anything about the %if %then result. Here is part of the code:

 

%MACRO tabulate(data, target, comp);

   /* ................datalines........................ */

 

  data data1 (replace=yes);

           set &data;

          %if "&comp._&target._result"="Positive" and "V_&target._result"="Positive" %then result="TP";

          %else %if "&comp._&target._result"="Positive" and "V_&target._result"="Negative" %then result="FN";

           %else %if "&comp._&target._result"="Negative" and "V_&target._result"="Positive" %then result="FP";

           %else %if "&comp._&target._result"="Negative" and "V_&target._result"="Negative" %then result="TN";

     run;

 

/*................ more data ............*/

%MEND;

 

 

I also tried to use %then %put result = "TP", still not working. This part of the code should give me a new column with name "result", and have "TP" "FN" "FP" "TN" in the column.

I can run this in the main program, but when I put this into a macro, it does not work.

Thank you if you can give me some advice.

1 ACCEPTED SOLUTION

Accepted Solutions
AndrewHowell
Moderator

Macro code is scanned & executed before data step code is scanned & executed.

 

It might be simpler to maintain if you separate the macro & data step code:

 

%MACRO tabulate(data, target, comp);

  /* ................datalines........................ */

%local result;
%if  &comp._&target._result=Positive and V_&target._result=Positive %then %let result=TP;
%else %if &comp._&target._result=Positive and V_&target._result=Negative %then result=FN;
%else %if &comp._&target._result=Negative and V_&target._result=Positive %then result=FP;
%else %if &comp._&target._result=Negative and V_&target._result=Negative %then result=TN;

  data data1 (replace=yes);
          set &data;
          result="&result";
  run;
 
/*................ more data ............*/

%MEND;

 

View solution in original post

3 REPLIES 3
Astounding
PROC Star

DATA steps can't execute macro statements.  Just get rid of all the percent signs and you should be fine.  (Leave the ampersands in place, only the percent signs need to go.)

AndrewHowell
Moderator

Macro code is scanned & executed before data step code is scanned & executed.

 

It might be simpler to maintain if you separate the macro & data step code:

 

%MACRO tabulate(data, target, comp);

  /* ................datalines........................ */

%local result;
%if  &comp._&target._result=Positive and V_&target._result=Positive %then %let result=TP;
%else %if &comp._&target._result=Positive and V_&target._result=Negative %then result=FN;
%else %if &comp._&target._result=Negative and V_&target._result=Positive %then result=FP;
%else %if &comp._&target._result=Negative and V_&target._result=Negative %then result=TN;

  data data1 (replace=yes);
          set &data;
          result="&result";
  run;
 
/*................ more data ............*/

%MEND;

 

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 773 views
  • 0 likes
  • 4 in conversation