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

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

Blue Blue
1 ACCEPTED SOLUTION

Accepted Solutions
Oligolas
Barite | Level 11

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 -

View solution in original post

14 REPLIES 14
ChrisNZ
Tourmaline | Level 20

This calls the macro called wednesday defined by

%macro wednesday();

 

Oligolas
Barite | Level 11

Hi,

It's just the way to call a macro inside another

%MACRO a(text=);
   %put %str(N)OTE: &param. &text.!;
%MEND a;

%MACRO b(param=);
   %if %lowcase(&param.) eq hello %then %a(text=Dear);
%MEND b;
%b(param=HelLO);
________________________

- Cheers -

GN0001
Barite | Level 11
Thanks for the response.
Are %lowcase, %if or %then or %put SAS automated macros?
CloudsIntheSky
Blue Blue
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
Tom
Super User Tom
Super User

%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

https://github.com/sasutils/macros/blob/master/lowcase.sas

GN0001
Barite | Level 11

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

Blue Blue
Tom
Super User Tom
Super User

@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
Barite | Level 11
Can there some functions or some keywords be converted into Macro only?
Please advise me.
Regards,
Clouds in Sky!
Blue Blue
Tom
Super User Tom
Super User

@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. ;
PaigeMiller
Diamond | Level 26

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.

 

--
Paige Miller
Tom
Super User Tom
Super User

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.

 

 

GN0001
Barite | Level 11
Hello team,
When we want to create a user defined macro, how do we know which components in a macro can take a percentage sign?
Are there a source for macro keywords and statements that can be used inside a Macro?
Thanks,
Blue Sky
Blue Blue
Oligolas
Barite | Level 11

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 -

GN0001
Barite | Level 11
Hello Oligolas,
I think your response is what I am looking for.
Thanks for all your effort.
I am trying to mark it as a solution, it seems someone has done it prior to me.
Respectfully,
Blue Sky!
Blue Blue

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 14 replies
  • 4533 views
  • 9 likes
  • 5 in conversation