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...
Simple Macro [2-5]


In this section, we will look at how to define a macro variable properly.

One easy way to define a macro variable is to use the %LET statement.
%let var1 = 123;

The %LET statement above defines a new macro variable called VAR1.

Its value is 123.
​

So, where is the value stored?

The value is stored in what we call a symbol table: 
Picture

A symbol table is a table that stores the macro values.

We cannot directly access this table as we do when accessing other data sets.

However, we can view the values using the %PUT statement.


%PUT Statement

The %PUT statement writes values to the SAS log.

Let's look at an example.
%put The weather is nice.;

The %PUT statement writes this text to the SAS log:

The weather is nice.

​On the SAS log, we see this:
Picture

In addition to plain text, we can display the macro variable value using the %PUT statement as well.

Example
%let var1 = 123;

%put The value of var1 is &var1;

​The %PUT statement above will write to the SAS log a combination of:
  1. Plain text
  2. Macro value of &var1
Picture

We see this on the SAS log:
Picture

The value of the macro variable VAR1 is displayed.


Before we continue, there is an important concept about macro variables that you need to know.


The macro variable value is always a text value

A macro variable does not distinguish between numeric or character. 

It is always a character value.

E.g.
  • Apple --> Character
  • 123 --> Character
Picture

SAS macro is mostly a text substitution tool.

It is used to dynamically substitute text in your program.

The value is always stored as a text value.

Exercise

Run the code below on your SAS editor:

%let computation = x + y;

data test;
x = 1;
y = 8;
z = &computation;
run;

What is the value of:
  1. Variable Z?
  2. Macro variable COMPUTATION?
Next

Need some help? 


HINT:
SAS Macro variable is always a text value. It does not do calculation.


SOLUTION:

The value of Z is 9. The value of COMPUTATION is X + Y.


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