Example: barber

SUGI 24: Macro Quoting Functions, Other Special Character ...

Macro Quoting functions , Other Special Character masking Tools, and How ToUse ThemArthur L. CarpenterCalifornia Occidental ConsultantsABSTRACTQ uoting functions allow the user to pass macroarguments while selectively removing the specialmeaning from characters such as , , and . Most of these functions are not commonlyused and are even less commonly understood. Although they are powerful and can even benecessary, usually programming solutions areavailable that do not require the use of thequoting are Quoting functions needed? Whenneeded how are they used? Which one shouldbe selected? This paper will discuss when to useand when to avoid Quoting functions . Inaddition the discussion will include solutions thatavoid the use of Quoting functions .

Macro Quoting Functions, Other Special Character Masking Tools, and How To Use Them Arthur L. Carpenter California Occidental Consultants ABSTRACT

Tags:

  Other, Special, Macro, Functions, Character, Masking, Quoting, Macro quoting functions, Other special character, Other special character masking

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of SUGI 24: Macro Quoting Functions, Other Special Character ...

1 Macro Quoting functions , Other Special Character masking Tools, and How ToUse ThemArthur L. CarpenterCalifornia Occidental ConsultantsABSTRACTQ uoting functions allow the user to pass macroarguments while selectively removing the specialmeaning from characters such as , , and . Most of these functions are not commonlyused and are even less commonly understood. Although they are powerful and can even benecessary, usually programming solutions areavailable that do not require the use of thequoting are Quoting functions needed? Whenneeded how are they used? Which one shouldbe selected? This paper will discuss when to useand when to avoid Quoting functions . Inaddition the discussion will include solutions thatavoid the use of Quoting functions .

2 Fortunatelythere are several ways to mask Special characterswithin the Macro language. These includequoting functions , %DO blocks, restructuring ofstatements, and the Character mask (%). KEY WORDS Macro , Quoting functions , masking , specialcharactersINTRODUCTIONC ertain characters or combinations of characterswill often cause the Macro language to behave inways that are neither desirable or anticipated. The programmer writing the SAS Macro mustanticipate these problems or code so thatproblems will not occur. Macro codingtechniques, Character masks, and Macro quotingfunctions all play a part in creating robust macrocode. Many of the examples and some of thetext in this paper are based on sections inCarpenter s Complete Guide to the SAS TO AVOID QUOTINGThe most commonly used Macro quotingfunction is %STR.

3 Often it is used along withthe %LET statement to mask semicolons thatwould otherwise terminate the % the following example we want to create amacro variable &P that contains two SASstatements;%LET P=PROC PRINT DATA=DSN; RUN;;Because the semicolon following DSNterminates the %LET statement, themacro variable &P contains:PROC PRINT DATA=DSNwhich results in a syntax error for themissing %STR function masks the semicolonby Quoting P=%STR(PROC PRINT DATA=DSN; RUN;);This results in the Macro variable &P beingcorrectly assigned the two PRINT DATA=DSN; RUN;In the following Macro the %STR function isused because multiple statements (withsemicolons) are needed in the DATA _NULL_step.

4 % Macro exist(dsn);%global exist;%if &dsn ne %then %str( data _null_; if 0 then set stop; run; );Advanced Tutorials%if &syserr=0 %then %letexist=yes; %else %do; %let exist=no; %put PREVIOUS ERROR USED TOCHECK FOR PRESENCE ; %put OF DATASET & IS NOT APROBLEM;%end;%mend exist; In the above Macro a %DO could have beenused instead of the %STR function. This resultsin code that I believe is easier to &dsn ne %then %do; data _null_; * No observations are actuallyread; if 0 then set stop; run;%end;As a general rule I would rather explorealternate coding methodologies like this onebefore resorting to Quoting functions . Anotheralternate to Quoting functions in somecircumstances is the use of the % Special CHARACTERSA number of symbols are usually expected inpairs.

5 These include the , , (, and ). Errorswill usually be created if only one half of the pairis specified in a string. This can especially be aproblem if you want to include one of thesemismatched symbols in a Macro of the Quoting functions expect thesesymbols to be in pairs %STR, others allowthem to be unpaired %BQUOTE. Whenyou need to use a mismatched symbol where itwill otherwise cause a problem, you can precedethe mismatched symbol with a % to mask the following example we want to assign thevalue (abcd to the Macro variable &B. TheLOG shows that the %STR is incorrectlyconstructed in line 40. The second openparenthesis causes the %STR to be closed by the) on line %let b = %str((abcd);41 * Unclosed );4243 %put (abcd); * UnclosedThis is corrected by using the % to mask themeaning of the second (.)

6 44 %let b = %str(%(abcd);45 * Unclosed );4647 %put (abcdA table showing several examples of the use ofthe % to mask characters can be found in on p. 81 in SAS Macro Language:Reference, First Edition and in Carpenter(1998,p. 94). Quoting FUNCTIONSQ uoting functions operate by adding what isessentially invisible characters before and afterthe string which is to be quoted. Once addedthese characters remain until stripped off by themacro processor (%UNQUOTE or by passingthe text to the SAS System for processing). There are two issues that make quotingfunctions more difficult to understand. Thesehave to do with functions that:Ceither do or don t rescan the text for %and & referencesCare executed at statement compilation orat statement execution Rescan and NoRescanOf Special interest in the text strings to bequoted are those that contain the Special macrosymbols % and &.)

7 Normally these symbolsindicate references to Macro calls and macrovariables that must be resolved prior to theexecution of the function. These references areresolved by rescanning the text as many times asit takes to resolve the usage of the % and &. Sometimes you do not want these referencesAdvanced Tutorialsresolved (you want to pass the Macro call ormacro variable without resolution). To do thisyou need a function designated as a NoRescanfunction (NR).Many of the Quoting functions come in pairs (thename either does or does not start with theletters NR %STR and %NRSTR. The twofunctions will perform essentially the sameoperation except the one with the NR will alsoremove the meaning from the % and &.)

8 Whenyou hear the jargon the meaning is removed ,this means that the Macro processor will not see the % and & as the Special characters thatit normally / ExecutionSince Macro statements are compiled and thenexecuted you may need to control when the textstring is to be quoted. Some Quoting functionsremove the meaning during compilation whileothers remove it during Macro execution. Whenmeaning is removed during compilation (%STRand %NRSTR) the resultant text is passed to theprocessor, but not the function call. The otherquoting functions are resolved during theexecution of the Macro . For these functions theentire call to the function and its text is passed tothe Macro processor where it is will rarely be an issue for mostprogrammers.

9 Usually you only will need toworry if text that is resolved during executionbecomes syntactically incorrect ormisinterpreted. Consider the following shortexample:%let type = or;%if &type ne xx %then %do;The Macro variable &TYPE is resolved duringthe evaluation of the expression which results in:%if or ne xx %then %do;The OR is seen as the logical mnemonic operatorand an error message is produced. If &TYPE isquoted during execution the resolved OR will betreated as text and there will not be a problemwith the type = or;%if %quote(&type) ne xx %then %do; Quoting FUNCTION OVERVIEWThe following table gives a fairly brief overviewof the Quoting functions and their Quoting functions are:%BQUOTE removes meaning from unanticipatedspecial characters (except & and %)during meaning from unanticipatedspecial characters including & and %during meaning from a string (except% and &) during meaning from %, &, specialcharacters, and mnemonics meaning from Special characters(except % and &) at meaning from Special charactersincluding % and & at any resolution of the value of amacro variable%UNQUOTE undoes NORESCANThe %NRSTR function behaves in the same wayas %STR except meaning is also removed fromthe % and &.

10 Macro variable references in the%STR are Tutorials%LET CITY = MIAMI;%PUT %STR(&CITY) IS ON THE WATER.;The LOG would show:MIAMI IS ON THE %NRSTR is used instead of %STR, themacro variable &CITY is not resolved becausethe Special meaning has been removed from the&.%LET CITY = MIAMI;%PUT %NRSTR(&CITY) IS ON THEWATER.;The LOG would show:&CITY IS ON THE on how the string containing theunresolved &CITY is used it might cause errorsor QUOTESB lind Quoting functions are used to removemeaning from unanticipated characters duringmacro execution. It is especially useful if thetext string was entered by a user through anapplication and you may not have been able totrap all possible characters that might cause themacro to that the Macro variable &METHOD hasbeen assigned the value:THE DOCTOR S NEW is very likely that when &METHOD isresolved the SAS System would produce anerror because of the unmatched single quote ( ).


Related search queries