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


Referencing Macro Variable II

In the last few sections, we have seen how we can reference a macro variable in values.

E.g.
  • Var = &var1;
  • name = "&name";

Macro variable can also be referenced in variable names.

E.g.
  • &var = "This is an example.";
  • Height_&name = 123;

​Example
%let name = Alfred;

data class;
set sashelp.class;
where name = "&name";

height_&name = height;

keep name height_&name;
run;

In this example, we have created a new variable called height_&name.
Picture

When executing the program, SAS will resolve the macro variable from HEIGHT_&NAME to HEIGHT_ALFRED. 

A brand-new variable called HEIGHT_ALFRED is created:
Picture


Separating Macro Variable Referencing with Text

In the above example, the variable name is specified as:

E.g. HEIGHT_&NAME

The macro variable &NAME is specified at the end of the variable name.

Now, what if we want the &NAME to be at the beginning of the variable name?

E.g. &NAME_HEIGHT

Do you think this will work?

Unfortunately, it won't.

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

data class;
set sashelp.class;
where name = "&name";

&name_height = height;

keep name &name_height;
run;

The variable is specified as &NAME_HEIGHT:
Picture

When running the program, we saw issues on the SAS log:
Picture

There is one warning message:

​WARNING: Apparent symbolic reference NAME_HEIGHT not resolved.

And one error message:
​
ERROR 180-322: Statement is not valid or it is used out of proper order.


So, what's the issue?

When referencing the macro variable, we have:

&NAME_HEIGHT

SAS does not recognize that it is a combination of macro variable reference (&NAME) and some additional text (-HEIGHT).

It treats the whole text (NAME_HEIGHT) as the macro variable reference!

For example, SAS will look up the symbol table and check if there is a macro variable called NAME_HEIGHT.

Since it cannot find it, it gives a warning message about macro variable reference not being resolved.

​Because of that, the new variable cannot be created.


Using a Period to Separate Macro Variable Reference and Text

In order to separate the macro variable reference (&NAME) with the text (_HEIGHT), we need to add a period between the two.

Example
%let name = Alfred;

data class;
set sashelp.class;
where name = "&name";

&name._height = height;

keep name &name._height;
run;

A period is added between &NAME and _HEIGHT:
Picture

SAS will resolve the macro variable reference from &NAME._HEIGHT to ALFRED_HEIGHT.

The ALFRED_HEIGHT variable is created:
Picture

Exercise

Create a new data set called CLASS_JANE from the SASHelp.Class data set.

The data set should have only one observation for Jane.

Create three new variables called:
  • JANE_HEIGHT
  • JANE_WEIGHT
  • JANE_AGE

​The text JANE in your program should be dynamically assigned using a macro variable.
Next

Need some help? 


HINT:
Be careful when creating a variable name that has a macro variable reference. Add a period to separate the reference from the text, if needed.


SOLUTION:

%let name = Jane;

data class_&NAME;
set sashelp.class;
where name = "&name";

&name._height = height;
&name._weight = weight;
&name._age = age;

keep name &name._height &name._weight &name._age;
run;


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