Example: biology

Resolving and Using &&var&i Macro Variables

1 Resolving and Using &&var&i Macro VariablesArthur L. CarpenterCalifornia Occidental ConsultantsABSTRACTOne of the most important aspects of the MacroSAS code is parsed, the Macro variable can beLanguage within the SAS System is its abilityused to store snippets of code, variable names, to create code dynamically. This allows theconstants, or even pieces of variable names. developer to write code that will do a series ofThese Macro Variables can then be joinedoperations without knowing exactly what thosetogether to form new and exciting combinations.

1 Resolving and Using &&var&i Macro Variables Arthur L. Carpenter California Occidental Consultants ABSTRACT One of the most important aspects of the Macro SAS code is parsed, the macro variable can be

Tags:

  Using, Macro, Variable, Resolving, Resolving and using amp amp var amp i macro variables

Information

Domain:

Source:

Link to this page:

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

Other abuse

Advertisement

Transcription of Resolving and Using &&var&i Macro Variables

1 1 Resolving and Using &&var&i Macro VariablesArthur L. CarpenterCalifornia Occidental ConsultantsABSTRACTOne of the most important aspects of the MacroSAS code is parsed, the Macro variable can beLanguage within the SAS System is its abilityused to store snippets of code, variable names, to create code dynamically. This allows theconstants, or even pieces of variable names. developer to write code that will do a series ofThese Macro Variables can then be joinedoperations without knowing exactly what thosetogether to form new and exciting combinations.

2 Operations will be at execution Macro Variables contain the name ofThis presentation will show how doubleother Macro Variables the resulting code willampersand Macro Variables are resolved and howoften contain two (or more!!) ampersands. Thecompound Macro Variables can be used to createfollowing sections address the issue of makingdynamic code. Examples include Using a list in asense of Macro Variables with multipleflat file to create a set of Macro Variables a series of or Macro Variables are stored inincluding Variables and data set names.

3 A periodmemory and are not associated with a data set asfollowing the Macro variable is assumed to be aare data set Variables whose values are stored inconcatenation operator for that Macro variable . the Program Data Vector for a particular dataPeriods before a Macro variable have no specialstep. This means that Macro Variables can besignificance. Double quotes are use when aused across DATA and PROC steps in a SASmacro variable is to be resolved inside of Variables are identified in SAS code bypreceding the Macro variable name with anampersand.

4 Thus the Macro variable DSN willWHERE SEX=" be coded as &DSN. TECHNICAL TIP: Professional SASprogrammers always refer to the ampersand as amper when reading code, and only noviceprogrammers pronounce the full first step in understanding Macro variablebehavior is to understand how Macro variablesare resolved. Symbolic variable resolution refersto the process of converting the contents of thevariable into symbols or code that can beprocessed as if it was entered as part of Macro Variables are resolved before theanother Macro variable or pieces of names ofMACRO variable RESOLUTIONA Macro variable may be appended to SAS code%LET SEX=MALE;DATA SET CLASS.

5 RUN;This code will resolve to:DATA MALEONLY;SET ;WHERE SEX="MALE";RUN;It is not uncommon within the SAS Languagefor some special characters, such as; singlequotes, periods and ampersands, to have dualmeanings. Dual use is indicated in the code byplacing two of the desired symbol. During theresolution of the Macro Variables the two2symbols are resolved to one. For example adouble .. (period) is used when a . (period) isdesired in the ' ' missover; %LET LIBREF=CLASS;DATA & ;SET SOMEDATA;RUN;This will resolve to:DATA ;SET SOMEDATA;RUN;A similar result is achieved when more than onemacro variable is joined to form a single DSN=CLINICS;%LET N=5;%LET DSN5=FRED;The Macro variable &DSN&Nresolves toCLINICS5, &DSN5 resolves to FRED.

6 The&&DSN&N combination first resolves to amacro variable (&DSN5) which then resolves toa value (FRED) in a second pass. DATA DEFINED Macro VARIABLESIt is not unusual to have a set of operations thatmust be performed on a series of data sets, Variables , or procedures. The control of thisprocess can easily be maintained by Using &&var&i operations. In the following example PROC FSEDIT is tobe run on each of a series of data sets. Thenames of the data sets are stored in a flat filenamed a portion of which lookslike:012_015 016_017 018 These data set names are placed into macrovariables Using the CALL SYMPUT routine in aDATA _NULL_ step.

7 * Load the names of the data sets; data _null_; length ii $2; input @1 dsn $8.; i+1; ii = left(put(i,2.)); call symput('n',ii); call symput('dsn'||ii,left(dsn)); run; The character variable ii assigns a sequentialnumber to each data set name. This number thenbecomes a part of the Macro variable name.

8 Inthis way &DSN2 is assigned the value of016_017. The total number of entries is alsoretained in & && Macro VARIABLEST ypically the Macro Variables created inapplications such as in the above example will beaddressed in the form of &&VAR&I whereVAR is the root portion of the variable name andthe index number is an integer Macro DO loop is often used to process eachof the Macro Variables . The code belowexecutes a PROC FSEDIT for each data setlisted in the text file in the previous example. Notice that the loop counts from 1 to &N, thetotal number of entries.

9 This means that if thelist changes all we have to modify is the q = 1 %to &n; PROC FSEDIT DATA= mod SCREEN= ; RUN;%end;Notice that three periods are required in theSCREEN= option because two passes areneeded to resolve the &&DSN&Q. The first passcompresses the three periods to two and thesecond pass compress the two periods to one. For &Q=2 this code resolves to: PROC FSEDIT DATA= mod SCREEN= ; RUN;3 DYNAMICALLY BUILDING SAS CODEO ften code must be written to be flexible inregard to what data sets or even how many data* count the number of keyvars sets are to be used.

10 Macro Variables can be usedto construct code that is independent of thisknowledge prior to execution. In the followingexample the SET statement is constructed frommacro ALL;SET%DO I = 1 %TO &N; ; Using the data set names from above this coderesolves to:DATA ALL;SET ;The following example assumes that we want toprocess each of the data sets in the list Using aBY statement. This is fairly easy unless the BYvariables differ for each data set. The Macro %KEYFLD creates the globalized macrovariable &KEYFLD which contains the keyvariables for the designated data set.


Related search queries