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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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