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
Sentry Page Protection
Please Wait...
SAS Macro [1-15]


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 subscribers for a gym membership 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 yours wants to get in touch with those who dropped out of the membership in less than 10 days.

You have written the following program to print the list of subscribers who dropped out early:

Code

title "Subscribers who dropped out in less than 10 days";

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

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
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:

Example

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 for a few more times, changing the duration at the different part of the program could become tedious. 

It is difficult to debug and easy to make mistake.

A better way to create a reusable program is to use the SAS macro variable instead:

Example

%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 value assigned to the macro variable (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? 

Get Hint
Get Solution

Fill out my online form.

Home 
​1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Summary | Final Test | Coding Exercises
Already a member? Go to member's area.