I am trying to write a macro to replace certain variables if condition is satisfied but it returns the original data to me. For example, for the data below, I want to make country=country2 and meal=meal2 if company="ABC" and source=2 (I want to make the second record to be (2 ABC Mary 12000 GERMANY GERMANT PIZZA PIZZA)
This is my dataset
data test;
input source company $ employee $ country $ country2 $ meal $ meal2;
datalines;
1 ABC Smith 10000 USA USA BURGER BURGER
2 ABC Mary 12000 USA GERMANY BURGER PIZZA
1 EFG Tim 11000 CANADA USE PIZZA PIZZA
;
run;
The macro function I tried to use to accomplish
%macro finalize;
data final;set test;
%if company="ABC" and source=2 %then %do;
country=country2;
meal=meal2;
%end;
run;
%mend;
%finalize;
But it returns the original dataset without changing anything and there was no error, I still have (2 ABC Mary 12000 USA GERMANT BURGER PIZZA) instead of my expected output of (2 ABC Mary 12000 GERMANY GERMANT PIZZA PIZZA). Can someone please let me know what is the reason? Thanks
%macro finalize;
data final;
set test;
if company="ABC" and source=2 then do;
country=country2;
meal=meal2;
end;
run;
%mend;
%finalize;
There's nothing in this logic requiring macro %if/%then. Use a standard if/then.
@apeeape wrote:
I am trying to write a macro to replace certain variables if condition is satisfied but it returns the original data to me. For example, for the data below, I want to make country=country2 and meal=meal2 if company="ABC" and source=2 (I want to make the second record to be (2 ABC Mary 12000 GERMANY GERMANT PIZZA PIZZA)
This is my dataset
data test; input source company $ employee $ country $ country2 $ meal $ meal2; datalines; 1 ABC Smith 10000 USA USA BURGER BURGER 2 ABC Mary 12000 USA GERMANY BURGER PIZZA 1 EFG Tim 11000 CANADA USE PIZZA PIZZA ; run;
The macro function I tried to use to accomplish
%macro finalize; data final;set test; %if company="ABC" and source=2 %then %do; country=country2; meal=meal2; %end; run; %mend; %finalize;
But it returns the original dataset without changing anything and there was no error, I still have (2 ABC Mary 12000 USA GERMANT BURGER PIZZA) instead of my expected output of (2 ABC Mary 12000 GERMANY GERMANT PIZZA PIZZA). Can someone please let me know what is the reason? Thanks
%macro finalize;
data final;
set test;
if company="ABC" and source=2 then do;
country=country2;
meal=meal2;
end;
run;
%mend;
%finalize;
There's nothing in this logic requiring macro %if/%then. Use a standard if/then.
@apeeape wrote:
I am trying to write a macro to replace certain variables if condition is satisfied but it returns the original data to me. For example, for the data below, I want to make country=country2 and meal=meal2 if company="ABC" and source=2 (I want to make the second record to be (2 ABC Mary 12000 GERMANY GERMANT PIZZA PIZZA)
This is my dataset
data test; input source company $ employee $ country $ country2 $ meal $ meal2; datalines; 1 ABC Smith 10000 USA USA BURGER BURGER 2 ABC Mary 12000 USA GERMANY BURGER PIZZA 1 EFG Tim 11000 CANADA USE PIZZA PIZZA ; run;
The macro function I tried to use to accomplish
%macro finalize; data final;set test; %if company="ABC" and source=2 %then %do; country=country2; meal=meal2; %end; run; %mend; %finalize;
But it returns the original dataset without changing anything and there was no error, I still have (2 ABC Mary 12000 USA GERMANT BURGER PIZZA) instead of my expected output of (2 ABC Mary 12000 GERMANY GERMANT PIZZA PIZZA). Can someone please let me know what is the reason? Thanks
Thanks a lot that works! I am very confused when using macro haha
%if company="ABC" and source=2 %then %do;
%IF %THEN %DO works on macro variables. It cannot access values of data step variables, and so it cannot test to see if company="ABC"
IF THEN DO works on data step variables and constants. It can test to see if the value of a data step variable is equal to something.
There is also no obvious need for a macro here.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.