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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
%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


 

View solution in original post

4 REPLIES 4
Reeza
Super User
%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


 

apeeape
Fluorite | Level 6

Thanks a lot that works! I am very confused when using macro haha

PaigeMiller
Diamond | Level 26
%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.

--
Paige Miller
apeeape
Fluorite | Level 6
Thanks for the information! Lesson learned 🙂

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 722 views
  • 2 likes
  • 3 in conversation