SAS Macro Variable

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

SAS Macro Variable

I couldn't understand meaning of the line- "A macro variable is like a standard data variable except that it does not belong to a data set and has only a single value which is always character. The value of a macro variable could be a variable name, a numeral, or any text you want substituted in your program. "

 

At one side, it is saying that macro variable has only a single value which is always a character and on the other side it is saying that value of macro variable can be a name or numeral. Isn't it a contradiction? Please explain.

 

I found the above line in 'SAS Macro Programming for Beginners' pdf ("http://www2.sas.com/proceedings/sugi29/243-29.pdf")

 


Accepted Solutions
Solution
‎02-03-2017 04:17 AM
Super User
Posts: 5,085

Re: SAS Macro Variable

Take a simple case:

 

%let year = 2017;

 

Is &YEAR numeric or character?  

 

The answer:  it is a set of 4 characters.  But it might be treated as numeric depending on how it is used.  For example, the usage might be in the context of a DATA step:

 

data fy&year;

set raw&year.data;

age = &year - year_of_birth;

run;

 

The first two instances of &YEAR get treated as character.  The final instance will get treated as character by macro language.  After substituting 2017 for &YEAR, the final statement becomes:

 

age = 2017 - year_of_birth;

 

When SAS language runs this statement, it treats 2017 as numeric.  But that decision gets made by interpreting the SAS language statements.  It does not get made by macro language.

View solution in original post


All Replies
Super Contributor
Posts: 336

Re: SAS Macro Variable

 

When you run a SAS-program ("run;"), SAS scans for macro statements and variables ("%let" and "&xx."). If it finds macro-code the SAS-macro-processor resolves the macro-statements, this is it replaces for example "&var." with the content of &var or it increments a macro-variable "%eval(&var.+1)" or something else. Eventually, the SAS-program - with the resolved macro variables (!) - is executed.

Solution
‎02-03-2017 04:17 AM
Super User
Posts: 5,085

Re: SAS Macro Variable

Take a simple case:

 

%let year = 2017;

 

Is &YEAR numeric or character?  

 

The answer:  it is a set of 4 characters.  But it might be treated as numeric depending on how it is used.  For example, the usage might be in the context of a DATA step:

 

data fy&year;

set raw&year.data;

age = &year - year_of_birth;

run;

 

The first two instances of &YEAR get treated as character.  The final instance will get treated as character by macro language.  After substituting 2017 for &YEAR, the final statement becomes:

 

age = 2017 - year_of_birth;

 

When SAS language runs this statement, it treats 2017 as numeric.  But that decision gets made by interpreting the SAS language statements.  It does not get made by macro language.

New Contributor
Posts: 3

Re: SAS Macro Variable

Thank you so much!
Super User
Super User
Posts: 7,407

Re: SAS Macro Variable

Think of macro variables and macro code as Find/Replace commands from your usual Word or similar application.  That is basically all it does it find and replace those macro parts of your code with actual Base SAS code.  It is the Base SAS code which goes into the compiler and is executed by the SAS system.  Macro is nothing more than an additional tool which can save you some typing and is never needed at any point, but can occasionally be useful.  

SAS Super FREQ
Posts: 8,743

Re: SAS Macro Variable

Hi:
I agree with the Find/Replace analogy. I joke with my students that the Macro Facility is just like a typewriter that is typing code for you except that along the way, if you use special macro variables, the Macro facility will do the equivalent of "find and replace" on the macro "triggers" that it finds.

As explained above, with the &YEAR example, this statement:
%let year = 2017;

is creating a macro variable of 4 characters. Those 4 characters happen to be numbers that for some reason need to be typed into a program by the Macro facility. When the macro variable is created and stored, it is stored as what you want to have appear in code when &year is used, as the 4 characters "2017". With a program like this:

data fy&year;
set raw&year.data;
age = &year - year_of_birth;
run;

followed by
proc print data=fy&year;
title "Age as of &year";
run;

There are a lot of different ways that &year is used -- and each time that macro variable is causing a find and replace over and over, every time &year is encountered by the Macro processor, the characters 2017 will be typed. THEN, when the final program without any & references goes to be compiled and executed, the full resolved code is this:
data fy2017;
set raw2017.data;
age = 2017 - year_of_birth;
run;

proc print data=fy2017;
title "Age as of 2017";
run;

Note how, in the resolved code -- this is the code that is actually being sent to the compiler-- ALL the references to &year are gone and they have been replaced by the typing of the value 2017. In some places in the program, the 2017 becomes part of a data set name; in other places in the program the 2017 becomes part of an assignment statement as a numeric constant; in other places in the program, 2017 is treated as text in a TITLE statement.

When you type the number 1234 on your keyboard or type the letters abcd, you are just pressing keys -- the application or window that is USING what you type determines how to treat what you've typed.

There's a longer explanation of macro basics in this paper: https://support.sas.com/resources/papers/proceedings13/120-2013.pdf

Hope this helps,

cynthia

New Contributor
Posts: 3

Re: SAS Macro Variable

Thanks a ton! 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 201 views
  • 2 likes
  • 5 in conversation