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)
%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); Run;
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 , . ( & ! $ * ) ; – / %.
%LET STR=Analytic Vidhya.Educon; %LET ABC=%SCAN(&STR,2); %LET BCD=%SCAN(&STR,1,i); %PUT &ABC; %PUT &BCD;
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)
%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.
%SYSFUNC (Function(arguments..) [, Format])
It has two arguments, first is for the function and second is optional to format the output of function.
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)
This functions works exactly as %STR, but it also masks macro triggers % and &.
%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.
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.