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")
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.
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.
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.
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.
Thanks a ton!
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.