Using Macro Functions to Manipulate Character Strings

Macro Character Functions

Often when working with macro variables, you need to manipulate character strings. You can do this by using macro character functions. With macro character functions, you can do the following:
  • 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, and more.
Macro character functions have the same basic syntax as the corresponding DATA step functions, and they yield similar results. 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. The next few sections explore several macro character functions in greater detail.

The %UPCASE Function

The %UPCASE function enables you to change the value of a macro variable from lowercase to uppercase before substituting that value in a SAS program. Since most comparison operators in the SAS language are case sensitive, it is often necessary to change values to uppercase.
General form, %UPCASE function:
%UPCASE (argument)
Here is an explanation of the syntax:
argument
is a character string.

Example

The Sasuser.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:
%let paidval=n;
proc means data=sasuser.all sum maxdec=0;
   where paid="&paidval";
   var fee;
   class course_title;
title "Uncollected Fees for Each Course";
run;
Table 9.8 SAS Log
163 %let paidval=n;
164  proc means data=sasuser.all sum maxdec=0;
165       where paid="&paidval";
166       var fee;
167       class course_title;
168  title "Uncollected Fees for Each Course";
169  run;

NOTE: No observations were selected from data set SASUSER.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.
Now we can use the %UPCASE function in the WHERE statement:
%let paidval=n;
proc means data=sasuser.all sum maxdec=0;
   where paid="%upcase(&paidval)";
   var fee;
   class course_title;
title "Uncollected Fees for Each Course";
run;
You can see that this time the WHERE expression does find matching observations.
uncollected fees for each course

The %QUPCASE Function

If the argument contains a special character, a mnemonic operator, or a macro trigger, you need to use the %QUPCASE function. %QUPCASE has the same syntax as the %UPCASE function, and it works the same as %UPCASE except that it also masks mnemonic operators and special characters (including macro triggers) in the function result.

Example

These statements show the results produced by %UPCASE and %QUPCASE:
%let a=begin;
%let b=%nrstr(&a);

%put UPCASE produces: %upcase(&b);
%put QUPCASE produces: %qupcase(&b);
In the first %PUT statement, the macro reference &b resolves to &a, which is converted to &A because of the %UPCASE function. Since the resolved value contains a macro trigger, it is treated as a macro variable reference and &A resolves to the value begin. The second %PUT statement uses the %QUPCASE function, which masks the ampersand in the resolved value of the macro variable b so that this value is not treated as another macro variable reference. Executing these statements produces the following messages in the SAS log.
Table 9.9 SAS Log
6    %let a=begin;
7    %let b=%nrstr(&a);
8
9    %put UPCASE produces: %upcase(&b);
UPCASE produces: begin
10   %put QUPCASE produces: %qupcase(&b);
QUPCASE produces: &A

The %SUBSTR Function

The %SUBSTR function enables you to extract part of a character string from the value of a macro variable.
General form, %SUBSTR function:
%SUBSTR (argument, position<,n>)
Here is an explanation of the syntax:
argument
is a character string or a text expression from which a substring is returned.
position
is an integer or an expression (text, logical, or mathematical) that yields an integer, which specifies the position of the first character in the substring.
n
is an optional integer or an expression (text, logical, or mathematical) that yields an integer that specifies the number of characters in the substring.
Note: If the length of n is greater than the number of characters following position in argument, %SUBSTR issues a warning message and returns a substring that contains the characters from position to the end of the string. If n is not specified, %SUBSTR also returns a substring that contains the characters from position to the end of the string.
For example, assume that the macro variable date has the value 05JAN2002.
  • The code %substr(&date,3) returns the value JAN2002.
  • The code %substr(&date,3,3) returns the value JAN.
  • The code %substr(&date,3,9) returns the value JAN2002 and produces a warning message.
The values of position and n 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.

Example

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 macro variable to determine the month and year. To start, we need to create an updated class schedule based on the data in sasuser.schedule, which is too old for this example:
* Update the class schedule based on previous ;
data update_schedule;
  set sasuser.schedule;
  begin_date + 3652;
run;
Next, we select observations from the updated schedule that are within the current month:
* Print a list of courses that started this month;
proc print data=update_schedule;
     where begin_date between
         "01%substr(&sysdate9,3)"d and
         "&sysdate9"d;
   title "All Courses Held So Far This Month";
   title2 "(as of &sysdate9)";
run;
courses held so far this month

The %QSUBSTR Function

If the argument contains a special character, a mnemonic operator, or a macro trigger, you need to use the %QSUBSTR function. %QSUBSTR has the same syntax as the %SUBSTR function, and it works the same as %SUBSTR except that it also masks mnemonic operators and special characters (including macro triggers) in the function result.

Example

These statements show the results produced by %SUBSTR and %QSUBSTR:
%let a=one;
%let b=two;
%let c=%nrstr(&a &b);

%put C: &c;
%put With SUBSTR: %substr(&c,1,2);
%put With QSUBSTR: %qsubstr(&c,1,2);
Executing these statements produces the following messages in the SAS log. As you can see, the first %PUT statement shows that &c resolves to the value &a &b. In the second %PUT statement, the %SUBSTR function extracts the value &a from the resolved value of the macro variable reference &c, and resolves &a to one. The third %PUT statement shows that the %QSUBSTR function prevents the value &a from being resolved further.
Table 9.10 SAS Log
11   %let a=one;
12   %let b=two;
13   %let c=%nrstr(&a &b);
14
15   %put C: &c;
C: &a &b
16   %put With SUBSTR: %substr(&c,1,2);
With SUBSTR: one
17   %put With QSUBSTR: %qsubstr(&c,1,2);
With QSUBSTR: &a

The %INDEX Function

The %INDEX function enables you to determine the position of the first character of a string within another string.
General form, %INDEX function:
%INDEX (source,string)
Here is an explanation of the syntax:
source and string
both are character strings or text expressions that can include
  • constant text
  • macro variable references
  • macro functions
  • macro calls.
The %INDEX function
  • 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

The following statements find the first character V in a string:
%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.
Table 9.11 SAS Log
The character v appears at position 3.

The %SCAN Function

The %SCAN function enables you to extract words from the value of a macro variable.
General form, %SCAN function:
%SCAN (argument, n<,delimiters>)
Here is an explanation of the syntax:
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, which 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.
delimiters
specifies an optional list of one or more characters that separate "words" or text expressions that yield one or more characters.
CAUTION:
If argument contains a comma, you must enclose argument 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.
The delimiters that %SCAN recognizes vary between ASCII and EBCDIC systems. If you omit delimiters, SAS treats the following characters as default delimiters:
  • ASCII systems: blank . < ( + & ! $ * ) ; ^ - / , % |
  • EBCDIC systems: blank . < ( + | & ! $ * ) ; ¬ - / , % ¦ ¢
If the delimiter list includes any of the default delimiters for your system, the remaining default delimiters are treated as text.

Example

You can use PROC DATASETS along with the %SCAN function and the SYSLAST macro variable to investigate the structure of the most recently created data set:
data work.thisyear;
   set sasuser.schedule;
    where year(begin_date) =
          year("&sysdate9"d);
run;

%let libref=%scan(&syslast,1,.);
%let dsname=%scan(&syslast,2,.);
proc datasets lib=&libref nolist;
title "Contents of the Data Set &syslast";
   contents data=&dsname;
run;
quit;
contents of WORK.THISYEAR

The %QSCAN Function

If the argument contains a special character, a mnemonic operator, or a macro trigger, you need to use the %QSCAN function. %QSCAN has the same syntax as the %SCAN function, and it works the same as %SCAN except that it also masks mnemonic operators and special characters (including macro triggers) in the function result.

Example

These statements show the results produced by %SCAN and %QSCAN:
%let a=one;
%let b=two;
%let c=%nrstr(&a*&b);

%put C: &c;
%put With SCAN: %scan(&c,1,*);
%put With QSCAN: %qscan(&c,1,*);
Executing these statements produces the following messages in the SAS log.
Table 9.12 SAS Log
47   %let a=one;
48   %let b=two;
49   %let c=%nrstr(&a*&b);
50
51   %put C: &c;
C: &a*&b
52   %put With SCAN: %scan(&c,1,*);
With SCAN: one
53   %put With QSCAN: %qscan(&c,1,*);
With QSCAN: &a
..................Content has been hidden....................

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