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 [3-5]


Referencing a Macro Variable

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

As seen in previous sections, we can reference a macro variable by putting an ampersand in front of the variable.

​Example
%let var1 = 123;

%put The value of macro variable var1 is &var1;

In this example, &VAR1 is the reference to the macro variable VAR1.

It displays the value of the macro variable which is 123.
Picture


Referencing a Macro Variable Within Quotations

When referencing a macro variable within quotations, you must use double quotes. 

Single quotes do not work.

Let's look at an example.
%let name = Alfred;

data list;
name_sin = '&name';
name_dou = "&name";
run;

In this example, we defined the macro variable NAME as Alfred.

We then define two variables that are intended to contain the name Alfred.
Picture

Please note that we used single quotes for the NAME_SIN variable and double quotes for the NAME_DOU variable:
Picture

The macro variable reference (&name) was not resolved when placed within single quotes:
Picture

Exercise

The SASHelp.fish data set contains a list of 159 fishes and their weight, length and height.

Write a SAS program to compute the mean and standard deviation of the weight of the fish for Whitefish.

The value Whitefish should be dynamically assigned using a macro variable.
Next

Need some help? 


HINT:
Make sure you use double quotes when referencing a macro variable.


SOLUTION:

%let species = Whitefish;

proc means data=sashelp.fish;
var weight;
where species = "&species";
run;


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