BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tianerhu
Pyrite | Level 9

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 ?

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
tianerhu
Pyrite | Level 9
Thank you for your help.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

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