@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.
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);
@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.
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!
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.