Using SAS Macro Functions to Manipulate Character Strings

Macro Character Functions

Often when working with macro variables, you need to manipulate character strings. Here are ways you can do this by using macro character functions:
  • change lowercase letters to uppercase
  • produce a substring of a character string
  • extract a word from a character string
  • determine the length of a character string
Macro character functions have the same basic syntax as the corresponding DATA step functions, and they yield similar results. However, it is important to remember that although they might be similar, macro character functions are distinct from DATA step functions. As part of the macro language, macro functions enable you to communicate with the macro processor in order to manipulate text strings that you insert into your SAS programs.
Note: You cannot use SAS data set variables in macro functions.

The %UPCASE Function

A Brief Overview

The %UPCASE function converts lowercase characters in the argument to uppercase. %UPCASE does not mask special characters or mnemonic operators in its result, even when the argument was previously masked by a macro quoting function.
%UPCASE is useful in the comparison of values because the macro facility does not automatically convert lowercase characters to uppercase before comparing values.

%UPCASE Syntax

Syntax, %UPCASE function:
%UPCASE(character-string|text-expression)
To convert characters to lowercase, use the %LOWCASE or %QLOWCASE autocall macro.

Example: Using the %UPCASE Function

The Certadv.All data set contains student information and registration information for computer training courses. Suppose, you want to create a summary of the uncollected course fees. This example shows how to do that.
%let paidval=n;
title "Uncollected Fees for Each Course";
proc means data=certadv.all sum maxdec=0;
   where paid="&paidval";
   var fee;
   class course_title;
run;
The following is written to the SAS log.
Log 8.1 SAS Log
163 %let paidval=n;
164  title "Uncollected Fees for Each Course"
165  proc means data=certadv.all sum maxdec=0;
166       where paid="&paidval";
167       var fee;
168       class course_title;
169  run;
NOTE: No observations were selected from data set CERT.ALL.
Because the value of the macro variable Paidval was specified in lowercase, the WHERE expression finds no matching observations. All the values of the data set variable Paid are stored in uppercase. Use the %UPCASE function in the WHERE statement.
%let paidval=n;
title "Uncollected Fees for Each Course";
proc means data=certadv.all sum maxdec=0;
   where paid="%upcase(&paidval)";
   var fee;
   class course_title;
run;
Output 8.1 PROC MEANS Output of Certadv.All
PROC MEANS Output of Certadv.All

The %SUBSTR Function

A Brief Overview

The %SUBSTR function produces a substring of character string (argument) by extracting the specified number of characters (length) beginning at the specified starting position. %SUBSTR does not mask special characters or mnemonic operators in its result, even when the argument was previously masked by a macro quoting function.
Note: If length is not specified, %SUBSTR returns a substring containing the characters from position to the end of argument. If length is greater than the number of characters following position, %SUBSTR issues a warning message and returns a substring that contains the characters from position to the end of argument.

%SUBSTR Syntax

Syntax, %SUBSTR function:
%SUBSTR(argument, position, <,length>)
argument
is a character string or a text expression. If argument might contain a special character or mnemonic operator, use %QSUBSTR.
position
is an integer or an expression (text, logical, or arithmetic) that yields an integer, That integer specifies the position of the first character in the substring. If position is greater than the number of characters in the string, %SUBSTR issues a warning message and returns a null value. An automatic call to %EVAL causes n to be treated as a numeric value.
length
is an optional integer or an expression (text, logical, or arithmetic) that yields an integer. That integer specifies the number of characters in the substring. If length is greater than the number of characters following position in an argument, %SUBSTR issues a warning message and returns a substring containing the characters from position to the end of the string. By default, %SUBSTR produces a string containing the characters from position to the end of the character string.

Example: Using the %SUBSTR Function

Suppose you want to print a report on all courses that have been taught since the start of the current month. You can use the %SUBSTR function and the SYSDATE9 automatic macro variable to determine the month and year.
proc print data=certadv.schedule;
     where begin_date between
         "30%substr(&sysdate9,3)"d and
         "&sysdate9"d;
   title "All Courses Held So Far This Month";
   title2 "(as of &sysdate9)";
run;
Output 8.2 PROC PRINT Output
PROC PRINT Output
Assume that the macro variable Date has the value 05JAN2017.
  • The code %substr(&date,3) returns the value JAN2017.
  • The code %substr(&date,3,3) returns the value JAN.
  • The code %substr(&date,3,9) returns the value JAN2017 and produces a warning message.
The values of position and length can also be the result of a mathematical expression that yields an integer. For example, %substr(&var,%length(&var)-1) returns the last two characters of the value of the macro variable Var.
Note: The %LENGTH function accepts an argument that is either a character string or a text expression. If the argument is a character string, %LENGTH returns the length of the string. If the argument is a text expression, %LENGTH returns the length of the resolved value. If the argument has a null value, %LENGTH returns 0.

The %INDEX Function

%INDEX Syntax

The %INDEX function enables you to determine the position of the first character of a string within another string.
Syntax, %INDEX function:
%INDEX (source, string)
source and string
both are character strings or text expressions that can include any of the following elements:
  • constant text
  • macro variable references
  • macro functions
  • macro calls
Here is what the %INDEX function does:
  • searches source for the first occurrence of string
  • returns a number representing the position in source of the first character of string when there is an exact pattern match
  • returns 0 when there is no pattern match

Example: Using the %INDEX Function

The following statements find the first character v in a string:
Note: %INDEX function is case sensitive.
%let a=a very long value;
%let b=%index(&a,v);
%put The character v appears at position &b;
Executing these statements writes the following line to the SAS log.
Log 8.2 SAS Log
The character v appears at position 3

The %SCAN Function

A Brief Overview

The %SCAN function searches the argument and returns the nth word. A word is one or more characters separated by one or more delimiters.
%SCAN does not mask special characters or mnemonic operators in its result, even when the argument was previously masked by a macro quoting function.
The %SCAN function allows character arguments to be null. Null arguments are treated as character strings with a length of zero. Numeric arguments cannot be null.
CAUTION:
If argument contains a comma, you must enclose it in a quoting function. Similarly, in order to use a single blank or a single comma as the only delimiter, you must enclose the character in the %STR function.
You can specify the characters to be used as delimiters (charlist). If you do not specify delimiters, %SCAN uses a default set of delimiters. These delimiters differ slightly between ASCII and EBCDIC systems. SAS treats the following characters as default delimiters:
  • ASCII systems: blank . < ( + & ! $ * ) ; / , % | ^
  • EBCDIC systems: blank . < ( + & ! $ * ) ; / , % | ¬ ¦ ¢
If you specify delimiters, all other characters, including default any delimiters not included in charlist, are treated as text.

%SCAN Syntax

Syntax, %SCAN function:
%SCAN (argument, n<,charlist<,modifiers>>)
argument
consists of constant text, macro variable references, macro functions, or macro calls.
n
is an integer or a text expression that yields an integer. That integer specifies the position of the word to return. If n is greater than the number of words in argument, the functions return a null string.
charlist
specifies an optional character expression that initializes a list of characters. This list determines which characters are used as the delimiters that separate words. The following rules apply:
  • By default, all characters in charlist are used as delimiters.
  • If you specify the K modifier in the modifier argument, then all characters that are not in charlist are used as delimiters.
Tip
You can add more characters to charlist by using other modifiers.
modifier
specifies a character constant, a variable, or an expression in which each non-blank character modifies the action of the %SCAN function. Blanks are ignored.

Example: Using the %SCAN Function

The following example illustrates using the %SCAN function to search argument and return the nth word.
%let a=one:two-three four;

%put First word is %scan(&a,1);
%put Second word is %scan(&a,2,:-);
%put Last word is %scan(&a,-1);
The following is written to the SAS log.
Log 8.3 SAS Log
First word is one:two
Second word is two
Last word is four
Last updated: October 16, 2019
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.55.198