BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
svasu611
Calcite | Level 5
HI Team,
 
why my macro not create new variable.
 
data s;
 set sashelp.class;
 if name="Alice" then
  new=1;
run;
%macro a;
 data a;
  set sashelp.class;
  %if name="Alice" %then
   %do;
    new=1;
   %end;
 run;
%mend;
%a;
 
 
Thanks in Advance.
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

@svasu611 wrote:
HI Team,
 
why my macro not create new variable.
 
data s;
 set sashelp.class;
 if name="Alice" then
  new=1;
run;
%macro a;
 data a;
  set sashelp.class;
  %if name="Alice" %then
   %do;
    new=1;
   %end;
 run;
%mend;
%a;
 
 
Thanks in Advance.

Because you completely misunderstand what the macro processor is, what it does, and when it does it.

 

The macro preprocessor is a sophisticated text generation engine that modifies code before it is fed to the SAS interpreter.

It only sees text, and the texts

name

and

"Alice"

(note the presence and non-presence of quotes, they are part of the text)

are not the same, so the code

new=1;

is not created by the macro, and can't be executed by the SAS interpreter.

 

Since you are a beginner, I strongly suggest you don't dabble with macros for the time being, and instead really learn how to work with the Base SAS code. Since macros generate dynamic code, it is essential to know what you create and what that will do in order to make proper use of the macro language.

Simple text replacement through macro variables is another thing, safe for beginners.

View solution in original post

3 REPLIES 3
SASKiwi
PROC Star

You can't substitute macro IF logic for DATA step IF logic. A macro IF can only compare values of macro variables not values held in a DATA step. A nonsensical version based on your example would be as follows. Since NAME can only be Alice, new will be 1 for all rows.

 

%macro a (name = );
 data a;
  set sashelp.class;
  %if &name=Alice %then
   %do;
    new=1;
   %end;
 run;
%mend;
%a (name = Alice);
Kurt_Bremser
Super User

@svasu611 wrote:
HI Team,
 
why my macro not create new variable.
 
data s;
 set sashelp.class;
 if name="Alice" then
  new=1;
run;
%macro a;
 data a;
  set sashelp.class;
  %if name="Alice" %then
   %do;
    new=1;
   %end;
 run;
%mend;
%a;
 
 
Thanks in Advance.

Because you completely misunderstand what the macro processor is, what it does, and when it does it.

 

The macro preprocessor is a sophisticated text generation engine that modifies code before it is fed to the SAS interpreter.

It only sees text, and the texts

name

and

"Alice"

(note the presence and non-presence of quotes, they are part of the text)

are not the same, so the code

new=1;

is not created by the macro, and can't be executed by the SAS interpreter.

 

Since you are a beginner, I strongly suggest you don't dabble with macros for the time being, and instead really learn how to work with the Base SAS code. Since macros generate dynamic code, it is essential to know what you create and what that will do in order to make proper use of the macro language.

Simple text replacement through macro variables is another thing, safe for beginners.

svasu611
Calcite | Level 5
Really Helpful for me Thank you so much all.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 691 views
  • 0 likes
  • 3 in conversation