BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aishmethi
Fluorite | Level 6

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")

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

6 REPLIES 6
user24feb
Barite | Level 11

 

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.

Astounding
PROC Star

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.

aishmethi
Fluorite | Level 6
Thank you so much!
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.  

Cynthia_sas
SAS Super FREQ
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

aishmethi
Fluorite | Level 6

Thanks a ton! 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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