Learn everything about Analytics

Home » Introduction to SAS Macros – Functions

Introduction to SAS Macros – Functions

In last 2 articles, we looked at the basic concept of SAS Macros and how they become useful to accomplish repetitive tasks easily. We also looked at how SAS Macros can be used in iterative and conditional circumstances. Now, let’s understand use of some Macro functions to manipulate text, perform arithmetic operations and execute SAS functions.


These Macro functions have similar syntax, compared to their counterpart functions in data steps and they also return results in similar manner. They can manipulate macro variables and expressions and these functions get evaluated only at stage of Macro processing. Most importantly, these do not require quotes around character constant arguments.

Let’s look at some of these one by one:-



This function is used to convert case of letters to uppercase:

Syntax: – %UPCASE (Argument)

Example: –

%Let heading=this is detail of country; 
 Proc Print Data=Orion.Country;
 Title %UPCASE(&heading);

It will print title as “THIS IS DETAIL OF COUNTRY”.



This function returns the given number of characters from specified position.

Syntax: – %SUBSTR (argument, position [, number of characters])

If number of character is not supplied, %SUBSTR function will return characters from given position till end of the string.


%Let abc=this is detail of country;
 %LET def=%SUBSTR(&abc,19,7);
 Proc Print Data=Orion.Country;
 Title %Upcase(&def);

Here we have first extracted “country” from macro variable abc and store it into def and after that used it to show title in upper case.



This function will return the nth word in a string (having n words separated by delimiters).

Syntax: – %SCAN (argument, n [, delimiter])

It will return NULL value, if string does not have n words separated by delimiter and if we have not given delimiter then it will use default value for it. Default delimiters are blank , .  (  & ! $ * ) ; – / %.

Example: –

%LET STR=Analytic Vidhya.Educon;
 %LET BCD=%SCAN(&STR,1,i);

ABC will store string “Vidhya”, here it automatically identify the second word based on default delimiter and BCD will have value “Analyt” because we have mentioned that i is a delimiter and we required the first word.



This function is used to perform mathematical and logical operation with macro variables. Remember, macro variable contains only text (numerical values are also stored as text) and therefore we can’t perform any arithmetical and logical operation, if we try to use them directly. EVAL() function also returns a text result.

Syntax: – %EVAL (Expression)

Example: –

%LET A=3 ;
 %LET B=&A+1;
 %LET C=%EVAL(&A+1);
 %PUT &A &B &C;

As we know, macro variables store value as text, macro variable B will store 3+1 and with the use of %EVAL, C will store 4.


If, we create a variable like:

 %LET D=%EVAL (&A+3.2);

What would variable D store?

If you answered 6.2, you are wrong! This statement will throw up error because SAS understands “.” as character. Now, to evaluate floating numbers, we will use %SYSEVALF () function and above statement should be written as:




This function is used to execute SAS functions in macro environment. With the help of this function, we can increase the list of functions available to the macro language significantly. This function can allow us to make almost all DATA step and user-written functions usable in SAS Macros.

Syntax: –

%SYSFUNC (Function(arguments..) [, Format])

It has two arguments, first is for the function and second is optional to format the output of function.

Example: –

%Put %SYSFUNC(Today(),Date9.);

This will display the current date in date9 format.



This function removes/masks the normal meaning of following token + – * /, > < = ; “ LT EQ GT LE GE LE NE AND OR NOT blank. It also preserves leading and trailing blanks of the string. Please note that this does not mask & and %.

Syntax: – %STR (argument)


%NSTR ()

This functions works exactly as %STR, but it also masks macro triggers % and &.

Example: –

%LET D=5;
 %LET abc = %STR ( "S &D Analytics");
 %PUT &abc;
%LET bcd = %NRSTR ( "S &D Analytics");
 %PUT &bcd;

Here first %PUT statement will return S 5 Analytics whereas second one will return S &D Analytics.


End Notes:

In this article, we looked at several functions to perform text manipulations and to apply arithmetical and logical operations in SAS Macros. These, in combination with conditional statements can provide a very powerful mechanism to automate / modularize your SAS programmes.

In the final article of this series, we will look at a few more ways to create Macro variables (apart from %LET).

Are you finding this series useful? We have decomposed a complex topic like SAS Macros and have tried to present it in simple and understandable manner. If you need any more help with SAS Macros, please feel free to ask your questions through comments below.

If you like what you just read & want to continue your analytics learningsubscribe to our emailsfollow us on twitter or like our facebook page.

You can also read this article on our Mobile APP Get it on Google Play
This article is quite old and you might not get a prompt response from the author. We request you to post this comment on Analytics Vidhya's Discussion portal to get your queries resolved


  • Praveen kumar.T says:

    Hi ,

    This is Praveen Kumar.T, working as S/W Engineer in RNTBCI,Chennai . I’m in a project which uses SAS windows and SAS Z/Os for Reporting purpose.Your articles on SAS MACROS are Clear and useful. Could you please post an article on comparision of SAS and R coding as it will be useful for people who needs to have clear picture in both technology.

  • Sumit says:


    “In the final article of this series, we will look at a few more ways to create Macro variables (apart from %LET).”

    I could not find the link to the final article being mentioned above. If you could share the link to that article (if at all it has been posted).


  • prateek sakhuja says:

    Hey Sumit, very well explained indeed. Have gone through your write ups for macros and you have made things really simple. I am facing some problems in understanding the complete use of SYMPUT. kindly help.
    thanx and regards.

  • prateek sakhuja says:

    Very well explained indeed. Have gone through your write ups for macros and you have made things really simple. I am facing some problems in understanding the complete use of SYMPUT. kindly help.
    Thanks and Regards,
    Prateek Sakhuja.