Download presentation
Presentation is loading. Please wait.
Published byHarold Horn Modified over 9 years ago
1
Copyright © 2008, SAS Institute Inc. All rights reserved. SAS ® Macros: Top-Five Questions (and Answers!) Kim Wilson –Technical Support Analyst SAS Institute Inc.
2
Copyright © 2008, SAS Institute Inc. All rights reserved. Q: Can you use SAS functions within the macro facility?
3
Copyright © 2008, SAS Institute Inc. All rights reserved. A: %SYSFUNC Macro Function The %SYSFUNC macro function enables you to execute SAS functions as well as user-written functions. In the following example, %SYSFUNC enables you to execute the DATE function: %put %sysfunc(putn(%sysfunc(date()),worddate20.)); This %PUT statement generates the following output: 95 %put %sysfunc(putn(%sysfunc(date()),worddate20.)); February 10, 2009
4
Copyright © 2008, SAS Institute Inc. All rights reserved. Q: What quoting function should I use to mask special characters such as the ampersand, percent sign, parentheses, and quotation marks?
5
Copyright © 2008, SAS Institute Inc. All rights reserved. A: Compile-Time Quoting Functions or Execution-Time Quoting Functions To mask special characters, one of the following types of quoting functions is required. Which type of quoting function you use depends on the situation: Compile-time quoting functions: %STR–masks commas, mnemonics, and unmatched quotation marks and parentheses %NRSTR–masks percent signs and ampersands Execution-time quoting functions: %BQUOTE–masks special characters and mnemonics in resolved values during macro execution %SUPERQ–masks special characters and mnemonics during macro execution. However, it prevents resolution of the value
6
Copyright © 2008, SAS Institute Inc. All rights reserved. Example 1: Unmatched Quotation Mark (‘) The following %LET statement contains a single quotation mark in the macro variable value: %let singleq=O'neill; %put &singleq; When you submit these statements, you do not receive any output because SAS expects a matching closing quotation mark.
7
Copyright © 2008, SAS Institute Inc. All rights reserved. Solution: Use the %STR Function To solve this problem, include the %STR function around the SINGLEQ macro variable’s value: %let singleq=%str(O%'neill) %put &singleq; Using %STR results in the following output: 3120 %put &singleq; O'neill
8
Copyright © 2008, SAS Institute Inc. All rights reserved. Example 2: Percent Sign (%) The following %LET statement contains a percent sign in the macro variable value: %let ex=This macro is called %showme; %put ex=&ex; When you submit these statements, SAS cannot resolve the macro invocation and you receive the following warnings: 20 %let ex= This macro is called %showme; WARNING: Apparent invocation of macro SHOWME not resolved. 21 %put ex=&ex; WARNING: Apparent invocation of macro SHOWME not resolved.
9
Copyright © 2008, SAS Institute Inc. All rights reserved. 23 %let ex=%nrstr(This macro is called %showme); 24 %put ex=&ex; ex=This macro is called %showme Solution: Use the %NRSTR Function To solve this problem, include the %NRSTR function around the SINGLEQ macro variable’s value: %let ex=%nrstr(This macro is called %showme); %put ex=&ex; Using %NRSTR results in the following output:
10
Copyright © 2008, SAS Institute Inc. All rights reserved. 31 %put WITH NO QUOTING FUNCTION:; WITH NO QUOTING FUNCTION: 32 %test(&x); ERROR: More positional parameters found than defined. Example 3: Commas (,) In the following %LET statement, commas are used as separators between the values A, B, and C : %macro test(value); %put &value; %mend; %let x=a,b,c; %put WITH NO QUOTING FUNCTION:; %test(&x); SAS interprets these commas as additional parameters, and you receive the following error message:
11
Copyright © 2008, SAS Institute Inc. All rights reserved. 34 %put WITH THE CORRECT QUOTING FUNCTION:; WITH THE CORRECT QUOTING FUNCTION: 35 %test(%bquote(&x)) a,b,c Solution: Use the %BQUOTE Function To solve this problem, mask the commas by using the %BQUOTE execution-time function: %put WITH THE CORRECT QUOTING FUNCTION:; %test(%bquote(&x)); Using %BQUOTE results in the following output:
12
Copyright © 2008, SAS Institute Inc. All rights reserved. In the following CALL statement, an ampersand is used to replace the word and in the value Beer&Brats : data _null_; call symputx('milwaukee','Beer&Brats'); run; %put NOT quoted:; %put &milwaukee; When you submit these statements, the macro processor interprets the ampersand as an indication that the name following it is a symbolic reference for a macro variable entry. The processor cannot find the entry in the macro symbols table, so you receive the following warning: 5 %put NOT quoted:; NOT quoted: 6 %put &milwaukee; WARNING: Apparent symbolic reference BRATS not resolved. Beer&Brats Example 4: Ampersand (&)
13
Copyright © 2008, SAS Institute Inc. All rights reserved. 8 %put THIS is quoted:; THIS is quoted: 9 %put %superq(milwaukee); Beer&Brats Solution: Use the %SUPERQ Function To solve this problem, include the %SUPERQ function around the macro variable: %put THIS is quoted:; %put %superq(milwaukee); %SUPERQ prevents the macro processor from attempting to resolve &Brats, and you receive the following output:
14
Copyright © 2008, SAS Institute Inc. All rights reserved. Q: Output that was generated with the MPRINT system option looks fine, so why are errors generated?
15
Copyright © 2008, SAS Institute Inc. All rights reserved. Quoting functions, such as the %STR function in the following example, can cause problems with output generated with the MPRINT option: options mprint; %macro test; %let val=aaa; %let test = %str(%'&val%'); data _null_; val = &test; put val=; run; %mend test; %test The output generated is as follows: Note: Line generated by the macro variable “TEST”. 1 ‘aaa’ - 386 --- 202 ERROR 386-185: Expecting an arithmetic expression. ERROR 202-322: The option or parameter is not recognized and will be ignored.
16
Copyright © 2008, SAS Institute Inc. All rights reserved. To solve this problem, use the %UNQUOTE function: options mprint; %macro test; %let val = aaa; %let test = %unquote(%str(%'&val%')); OR data _null_; val = %unquote(&test); put val=; run; %mend test; %test This example outputs VAL=‘aaa’, and no error messages are generated. A: %UNQUOTE Function
17
Copyright © 2008, SAS Institute Inc. All rights reserved. Q: How do you conditionally execute a macro from within a DATA step?
18
Copyright © 2008, SAS Institute Inc. All rights reserved. A: CALL EXECUTE Routine Use the CALL EXECUTE routine to conditionally execute a macro from within a DATA step, as shown in this example: /* Compile the macro BREAK. The BYVAL */ /* parameter is generated in the CALL */ /* EXECUTE routine. */ %macro break(byval); data age_&byval; set sorted(where=(age=&byval)); run; %mend; proc sort data=sashelp.class out=sorted; by age; run; options mprint; data _null_; set sorted; by age; if first.age then call execute(‘%break(‘||age||’)’); run; (continued)
19
Copyright © 2008, SAS Institute Inc. All rights reserved. MPRINT(BREAK): data age_11; MPRINT(BREAK): set sorted(where=(age=11)); MPRINT(BREAK): run; MPRINT(BREAK): data age_12; MPRINT(BREAK): set sorted(where=(age=12)); MPRINT(BREAK): run; MPRINT(BREAK): data age_13; MPRINT(BREAK): set sorted(where=(age=13)); MPRINT(BREAK): run; MPRINT(BREAK): data age_14; MPRINT(BREAK): set sorted(where=(age=14)); MPRINT(BREAK): run; MPRINT(BREAK): data age_15; MPRINT(BREAK): set sorted(where=(age=15)); MPRINT(BREAK): run; MPRINT(BREAK): data age_16; MPRINT(BREAK): set sorted(where=(age=16)); MPRINT(BREAK): run; A: CALL EXECUTE Routine Output generated by the previous program:
20
Copyright © 2008, SAS Institute Inc. All rights reserved. Q: Can I use DATA step variables in a %IF-%THEN statement?
21
Copyright © 2008, SAS Institute Inc. All rights reserved. A: Data Step Variables Cannot Be Used in %IF- %THEN Statements In the following example, the %IF condition contains the DATA step variable named TEXT: %macro test; data sample; text=“OPEN”; %if TEXT=OPEN %then %do; %put TRUE?; %end; run; %mend; This is a literal comparison, and TEXT=OPEN will never be true.
22
Copyright © 2008, SAS Institute Inc. All rights reserved. This statement is the part of the SAS macro language that conditionally generates text. An expression can only contain operands that are constant text or text expressions that generate text. This statement cannot refer to DATA step variables. This statement executes during macro execution. If the statement is contained in a DATA step, it is executed before DATA step compilation. This statement is the part of the SAS language that conditionally executes SAS statements during DATA step execution. An expression can only contain operands that are DATA step variables, character or numeric constants, or date and time constants. This statement executes during DATA step execution. If the statement is contained within a macro, it is stored as text. %IF-%THEN Statement IF-THEN Statement Comparison of %IF-%THEN Statement with IF-THEN statement
23
Copyright © 2008, SAS Institute Inc. All rights reserved. Recommended Reading Burlew, Michelle. 2006. SAS Macro Programming Made Easy, Second Edition. Cary, NC: SAS Press. Carpenter, Art. 2004. Carpenter's Complete Guide to the SAS ® Macro Language, Second Edition. Cary, NC: SAS Press. Tyndall, Russ. Give Your Macro Code an Extreme Makeover: Tips for even the most seasoned macro programmer. Cary, NC: SAS Institute Inc. Available at support.sas.com/techsup/technote/ts739.pdf.
24
Copyright © 2008, SAS Institute Inc. All rights reserved. Questions?
25
Copyright © 2008, SAS Institute Inc. All rights reserved.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.