Search the site...

SASCRUNCH TRAINING
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
Simple Macro [1-5]


Text Substitution
You can substitute text in your program using SAS macro variables.

Example

Copy and run the SUBSCRB data set from the yellow box below. 
Picture

The SUBSCRB data set contains a list of gym members with the following variables:
  • SID: Subscribers ID
  • StartDate: the start date of the gym membership
  • EndDate: the end date of the gym membership
  • Duration: the length of the membership in day(s)

Now, a manager of the gym wants to get in touch with those who cancelled their membership within 10 days of signing up.

You have written the following program to print the list of members who dropped out early:
title "Subscribers who dropped out in less than 10 days";

proc print data=subscrb;
where duration < 10;
run;

The program prints the list of subscribers whose length of membership is less than 10 days. 

It also has the title that describes this list of subscribers.
Picture
Picture
Now, what if you want to print the subscribers whose duration is less than 20 days?

You just have to write the exact same program but with the new duration:
title "Subscribers who dropped out in less than 20 days";

proc print data=subscrb;
where duration < 20;
run;
Picture

This definitely does the job.

However, if you want to reuse the program a few more times, changing the duration in different parts of the program could become tedious. 

It is difficult to debug the program and easy to make a mistake.

A better way to create a reusable program is to use the SAS macro variable instead:
%let dur = 20;

title "Subscribers who dropped out in less than &dur days";

proc print data=subscrb;
where duration < &dur;
run;
Picture

The SAS macro variable (dur) was assigned the value of 20.
Picture

​It is then inserted into your program where the macro variable was referenced (&dur):
Picture

When the program is executed, the macro variable reference (&dur) will be replaced by the macro variable value (i.e. 20).

This will be the same as executing the following program instead:
Picture

Substituting text with macro variables is fairly easy.

In the next few sessions, you will learn a few more examples of how macro variables can make your program more dynamic and reusable.

Exercise

Reuse the program above and print the list of subscribers whose length of membership is less than 30 days.
Next

Need some help? 


HINT:
You can assign a different value to the macro variable at the top of the program and keep the rest of the program intact.


SOLUTION:
%let dur = 30;

​​title "Subscribers who dropped out in less than &dur days";

proc print data=subscrb;
where duration < &dur;
run;


Fill out my online form.
Already a member? Go to member's area.