Hello team,
I have a macro, inside the macro I have this statement:
%do i= 2005 %to 2010
Or percentage sign in this:
%if &sysday=Wednesday %then %Wednesday
Can you please explain why we put % sign in statements inside a macro. I know that a macro or a variable macro starts with %. Why do the components inside a macro get a percentage sign?
Regards,
CloudsInSky
Yes, there is.
By Pressing F1 in your SAS System you should be able to open the SAS help and consult "SAS Products">"Base SAS">"SAS 9.x Macro Language Reference"
Online you'll find the SAS® 9.4 Macro Language: Reference, Fifth Edition
I guess the points Macro Statements and Macro Functions are mainly what you're looking for
- Cheers -
This calls the macro called wednesday defined by
%macro wednesday();
Hi,
It's just the way to call a macro inside another
%MACRO a(text=);
%put %str(N)OTE: ¶m. &text.!;
%MEND a;
%MACRO b(param=);
%if %lowcase(¶m.) eq hello %then %a(text=Dear);
%MEND b;
%b(param=HelLO);
- Cheers -
@GN0001 wrote:
Thanks for the response.
Are %lowcase, %if or %then or %put SAS automated macros?
CloudsIntheSky
Technically, these are macro functions (%lowcase) or macro statements (%if, %then, %put). They are not macros.
%IF is a macro statement. %THEN is a macro keyword that is part of the %IF statement.
%PUT is macro statement.
%LOWCASE is a SAS supplied autocall macro. You can see where it lives by turning on the MAUTOLOCDISPLAY option.
2354 %put %lowcase(Hello); MAUTOLOCDISPLAY(LOWCASE): This macro was compiled from the autocall file C:\Program Files\SASHome\SASFoundation\9.4\core\sasmacro\lowcase.sas hello
You can even see the code for the macro by looking at that file:
%macro lowcase(string); %*********************************************************************; %* *; %* MACRO: LOWCASE *; %* *; %* USAGE: 1) %lowcase(argument) *; %* *; %* DESCRIPTION: *; %* This macro returns the argument passed to it unchanged *; %* except that all upper-case alphabetic characters are changed *; %* to their lower-case equivalents. *; %* *; %* E.g.: %let macvar=%lowcase(SAS Institute Inc.); %*; %* The variable macvar gets the value "sas institute inc." *; %* *; %* NOTES: *; %* Although the argument to the %UPCASE macro function may *; %* contain commas, the argument to %LOWCASE may not, unless *; %* they are quoted. Because %LOWCASE is a macro, not a function, *; %* it interprets a comma as the end of a parameter. *; %* *; %*********************************************************************; %sysfunc(lowcase(%nrbquote(&string))) %mend;
Since it is NOT part of the macro language you are free to create a better version of the macro if you want. For example one that does not complain if you have commas in the string you want to lowercase.
2368 %put %lowcase(A,b); MAUTOLOCDISPLAY(LOWCASE): This macro was compiled from the autocall file C:\Program Files\SASHome\SASFoundation\9.4\core\sasmacro\lowcase.sas ERROR: More positional parameters found than defined. 2369 %macro lowcase/parmbuff; 2370 %if %length(%superq(syspbuff)) %then %sysfunc(lowcase&syspbuff); 2371 %mend lowcase; 2372 %put %lowcase(A,b); a,b
Thanks for the response.
Then we can convert anything, any word, any statement or any function in SAS by placing percentage sign before it.
Please advise me.
CloudsIntheSky
@GN0001 wrote:
Thanks for the response.
Then we can convert anything, any word, any statement or any function in SAS by placing percentage sign before it.
Please advise me.
CloudsIntheSky
No. That is exactly backwards. There is a very specific syntax for the macro language as specified in the documentation.
@GN0001 wrote:
Can there some functions or some keywords be converted into Macro only?
Please advise me.
Regards,
Clouds in Sky!
Not sure I get what you are asking here. If you want to create a macro just use %MACRO and %MEND statement. The actual SAS code that the macro is being used to generate does not change just because a macro is generating it.
If you are talking about the %SYSFUNC() macro function then in general most SAS functions can be called by it. There are some exceptions. The most common thing is that you cannot use it with INPUT() or PUT() functions. Instead you must use one the type specific versions INPUTN(), INPUTC(), PUTN() and PUTC(). And it really makes no sense to try to use the CAT...() series of function with it. If you want to concatenate text in macro code just put the text where you want it.
%let dsn=&libref..&memname. ;
I know that a macro or a variable macro starts with %.
A macro is CALLED by a percent sign and the macro name. Example: %dothis
A macro starts with %MACRO and ends with %MEND
A macro variable starts with an ampersand. Example: &flag
Why do the components inside a macro get a percentage sign?
To distinguish between text and macro language elements. In your example code, Wednesday is text. %wednesday is a call to a macro.
The macro processor is a text processing language that evaluates the text of your program and changes it before passing the resulting text onto SAS to interpret and run. There are a limited number of predefined macro statements and macro functions and you have the ability with the %MACRO and %MEND statements to define new macros.
So in your code %DO, %TO, %IF and %THEN are macro statements. And %WEDNESDAY is a call to a macro (instead of a native macro processor statement).
The special characters % and & are triggers to the macro processor to know when it should operate on the text. So the % are significant to the macro processor whether or not the code is inside a macro or not. Some macro code like %LET statements, macro function calls or macro calls can run outside of a user defined macro. You can now even run simple %IF/%THEN/%DO/%END/%ELSE/%DO/%END blocks in "open" code.
Yes, there is.
By Pressing F1 in your SAS System you should be able to open the SAS help and consult "SAS Products">"Base SAS">"SAS 9.x Macro Language Reference"
Online you'll find the SAS® 9.4 Macro Language: Reference, Fifth Edition
I guess the points Macro Statements and Macro Functions are mainly what you're looking for
- Cheers -
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.