This chapter gives you the tools to use macro variables
and write simple macros. Macros are particularly useful if you want to make your SAS
programs more flexible and allow them to be used in different situations without having
to rewrite entire programs.( from my textbook of SAS)
macro variables and macros are the same things ?
Macros and macro variables are different things.
A macro is a block of SAS code that can be called as needed to execute.
Inside macros (and also outside macros), macro variables are entities that have a value. One way to assign values to macro variables is via the %LET statement. You can change the value of a macro variable as needed, and you can have as many macro variables as needed.
Depending on the values of macro variables used by the macro, the code that is executed will change. In one run of the macro, it does one thing, in another run of the macro, it does a different thing.
A typical (but very simplified) example of a macro named EXTRACT which uses a macro variable called DATE
%macro extract;
proc sql;
create table abcd as select * from database.table where date>="&date"d;
quit;
title "Data Set ABCD starting at &date";
proc print data=abcd;
run;
%mend;
%let date=01MAR2021;
%extract
As you can imagine, if you change the value of DATE, you get a different extract from the database.
Macros and macro variables are different things.
A macro is a block of SAS code that can be called as needed to execute.
Inside macros (and also outside macros), macro variables are entities that have a value. One way to assign values to macro variables is via the %LET statement. You can change the value of a macro variable as needed, and you can have as many macro variables as needed.
Depending on the values of macro variables used by the macro, the code that is executed will change. In one run of the macro, it does one thing, in another run of the macro, it does a different thing.
A typical (but very simplified) example of a macro named EXTRACT which uses a macro variable called DATE
%macro extract;
proc sql;
create table abcd as select * from database.table where date>="&date"d;
quit;
title "Data Set ABCD starting at &date";
proc print data=abcd;
run;
%mend;
%let date=01MAR2021;
%extract
As you can imagine, if you change the value of DATE, you get a different extract from the database.
@tianerhu wrote:
macro variables and macros are the same things ?
No. Start here: Getting Started with the Macro Facility
then Replacing Text Strings Using Macro Variables
then Generating SAS Code Using Macros
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.