Download presentation
Presentation is loading. Please wait.
Published byWarren Sherman Modified over 9 years ago
1
I OWA S TATE U NIVERSITY Department of Animal Science Writing Flexible Codes with the SAS Macro Facility (Chapter in the 7 Little SAS Book) Animal Science 500 Lecture No. 7 September 21, 2010
2
I OWA S TATE U NIVERSITY Department of Animal Science SAS Macro Development u SAS Macros used to be utilized by “more advanced” SAS Users u Today SAS Macros are being used by more and more people n Including beginners n Using existing Macros n Developing your own Macros
3
I OWA S TATE U NIVERSITY Department of Animal Science General Macro Knowledge u Macros typically take longer to write and debug than a normal SAS program that you might only use a few times u After Macros are developed n Make developing maintaining a “production” program that is used a great deal easier 1. You can have SAS make a change in one part of your program and have SAS “echo” that throughout the rest of the program 2. Allows you to write a piece of SAS code and reuse it several times either the same day or days later
4
I OWA S TATE U NIVERSITY Department of Animal Science The Macro Processor u Compiling “submitting your code” your SAS program is different than when submitting a SAS Macro n Your standard SAS code is submitted and immediately executed n Macros have an additional step l SAS passes the Macro statement to the macro processor which “resolves” your macros that generates standard SAS code l Called meta programming because you write a program that writes a program
5
I OWA S TATE U NIVERSITY Department of Animal Science Macros and macro variables u SAS macro code has two parts 1. Macros 2. Macro variables u Name of the Macro is prefixed with a % u Name of the Macro variables is prefixed with an & n Is like your variable in a data set except it is not associated with a data set and it is always a character variable l Can be A variable name A numeral Any text you want substituted into your program
6
I OWA S TATE U NIVERSITY Department of Animal Science Macros and macro variables u Macro is a larger piece of program that may contain complex logic n Data n Proc Statements u Macro Statements n %Do n %End n %IF- n %Then/ n %Else
7
I OWA S TATE U NIVERSITY Department of Animal Science Macros u Macro variable can have different “scopes” n Local – defined within the macro n Global – macro code that is outside of the macro - Use global macro variable anywhere in your program - A local macro variable can only be used inside the macro - Cannot have a global and local macro variable with the same name.
8
I OWA S TATE U NIVERSITY Department of Animal Science Invoking or Turning on the Macro Processor u Must turn on the MACRO system option in order to use it n Macro is usually on by default n May be turned off – especially true on mainframe computer systems (usually seen in larger businesses, ISU has what some would consider a mainframe but not used by general students l SAS runs faster if it does not have to check and see if there are macros are present l If MACROS are turned off that means SAS does not have to check for them
9
I OWA S TATE U NIVERSITY Department of Animal Science Invoking or Turning on the Macro Processor u To check and see whether the MACRO option is on use the following SAS code: PROC OPTIONS OPTION = MACRO; RUN; QUIT; u Examine the SAS log n If you see 1. the option MACRO then the macro processor is turned on 2. NOMACRO the macro is turned off and you need to specify the MACRO option at invocation or in a configuration file * Specifiying this type of option is system dependent - check your SAS Help and Documentation for your operating system
10
I OWA S TATE U NIVERSITY Department of Animal Science Substituting Text with Macro Variables u Suppose you have a SAS program that you run once a week u Each time you run the program you have to edit the program so it will select data for the correct range of dates and print the correct dates in the title n Time consuming n Errors frequently happen and then are correct (even time consuming) or you send a report that has errors (bad image)
11
I OWA S TATE U NIVERSITY Department of Animal Science Substituting Text with Macro Variables u Can use a MACRO variable to insert the correct date; u Using the macros in this manner allows a much less experience person to generate the report – n The person does not have to know SAS code because the changes are made automatically
12
I OWA S TATE U NIVERSITY Department of Animal Science Substituting Text with Macro Variables u When SAS encounters the name of a macro variable, the macro processor replaces the name with the value of that macro variable. u Simplest way to do this: n Use the Macro called %Let n General form %Let macro-variable-name = value n The macro-variable-name must follow normal SAS rules l 32 characters or less l Start with a letter or _ l Contain only letters, numerals, or underscores.
13
I OWA S TATE U NIVERSITY Department of Animal Science Substituting Text with Macro Variables n General form %Let macro-variable-name = value n The macro-variable-name must follow normal SAS rules l 32 characters or less l Start with a letter or _ l Contain only letters, numerals, or underscores. n value is the text that is substituted for the macro variable name can be longer than you will ever need 64,000 characters long u The following statements each create a macro variable n % Let iterations = 10; n % Let country = New Zealand;
14
I OWA S TATE U NIVERSITY Department of Animal Science Substituting Text with Macro Variables u The following statements each create a macro variable n %Let iterations = 10; n %Let country = New Zealand; ** Notice that unlike ordinary assignment statement like infile, value does not need quotation marks (‘ ’ or “ ” ) ** Except for the blanks at the beginning and end, which are trimmed, everything between the equals sign and the semicolon becomes part of the value for that macro variable
15
I OWA S TATE U NIVERSITY Department of Animal Science Using a Macro Variable u To use a macro variable you just add the & sign (ampersand) and stick the macro variable name wherever you want its value to be substituted. u The macro processor doesn’t look for macros inside single quotation marks to get around this you must use double quotations “ ”. u Do i = 1 to &iterations; u TITLE “Addresses in &country”; u After being resolve by the macro processor, these statements would result in the following: u Do i = 1 to 10; (the 10 came from code on previous slide) u Title “Addresses in New Zealand”;
16
I OWA S TATE U NIVERSITY Department of Animal Science Adding Parameters to MACROS u Prolog n Macros allow you to use the same set of statements over and over n Parameters are macro variables whose value you set when you invoke a macro
17
I OWA S TATE U NIVERSITY Department of Animal Science Example u A macro named %QUARTERLYREPORT might start like n %MACRO quarterlyreport (quarter=, salesrep = ); n This macro has two parameters &QUARTER and &SALESREP. n You invoke the macro with the following statement n %quarterlyreport (quarter=3, salesrep=smith)
18
I OWA S TATE U NIVERSITY Department of Animal Science Example u Suppose the grower often needs a report showing sales to an individual customer and then sort the results. The data contain the customer ID, date of sale, variety of flower, and quantity.
19
I OWA S TATE U NIVERSITY Department of Animal Science Data -------+--------1--------+--------2--------+--------3 240W02-07-2000 GINGER 120 356W 02-08-2000 HELICONIA 60 356W 02-08-2000 ANTHURIUM 300 188R 02-11-2000 GINGER 24 188R 02-11-2000 ANTHURIUM 24 240W 02-12-2000 PROTEA 180 356W 02-12-2000 GINGER 240
20
I OWA S TATE U NIVERSITY Department of Animal Science Program %LET flowertype = Ginger; * Read all the flower sales data and subset with a macro variable; DATA flowersales; INFILE ‘c:\mydocuments\sales\sales.dat’; INPUT CustomerID $4 @6 Saledate MMDDYY10. @17 Variety $9. Quantity; IF Variety = “&flowertype”; Run; Quit; * Invoke a macro to Print the report using a macro variable PROC PRINT DATA = flowersales; FORMAT Saledate WORDDATE18.; TITLE “Sales of &flowertype”; Run; Quit;
21
I OWA S TATE U NIVERSITY Department of Animal Science Output from Macro Example Sales of Ginger Customer Obs IDSaleDateVarietyQuantity 1240WFebruary 7, 2008Ginger120 2188RFebruary 11, 2008Ginger 24 3356WFebruary 12, 2008Ginger240
22
I OWA S TATE U NIVERSITY Department of Animal Science Program * Macro with parameters %MACRO select (customer=, sortvar=); PROC SORT DATA = flowersales OUT = salesout; BY &sortvar; WHERE CustomerID = “&customer”; PROC PRINT DATA = salesout; FORMAT SaleDate WORDDATE18.; TITLE “Orders for Customer Number &customer”; %MEND select;
23
I OWA S TATE U NIVERSITY Department of Animal Science Output looks like this: Orders for Customer Number 356W 1 Customer OBS ID SalesDate Variety Quantity 1 356W February 8, 2000 Anthurium 300 2 356W February 12, 2000 Ginger 240 3 356W February 8, 2000 Heliconia 60 Orders for Customer Number 240W 2 Customer OBS ID SalesDate Variety Quantity 1 240W February 7, 2000 Ginger 120 2 240W February 12, 2000 Protea 180
24
I OWA S TATE U NIVERSITY Department of Animal Science Creating Modular Code with Macros u If you are writing the same or similar SAS statements over and over consider using a Macro. n You can package a piece of bug free code and use it repeatedly within the same SAS program or in many programs u %Macro and %Mend can be thought of as a sandwich n You can put anything you want
25
I OWA S TATE U NIVERSITY Department of Animal Science Creating Modular Code with Macros u %Macro and %Mend can be thought of as a sandwich n You can put anything you want n General form is: %MACRO macro-name; (tells SAS it’s the beginning of a Macro) macro-text %MEND macro-name; (tells SAS it’s the end of a Macro) Macro-name is a name you can make up up to 32 characters start with a letter or _ contain only letters, numerals, and underscores
26
I OWA S TATE U NIVERSITY Department of Animal Science Creating Modular Code with Macros u %Macro and %Mend can be thought of as a sandwich n You can put anything you want n General form is: %MACRO macro-name; (tells SAS it’s the beginning of a Macro) macro-text %MEND macro-name; (tells SAS it’s the end of a Macro) Macro-name is a name you can make up up to 32 characters start with a letter or _ contain only letters, numerals, and underscores
27
I OWA S TATE U NIVERSITY Department of Animal Science Creating Modular Code with Macros n General form is: %MEND macro-name; n The macro-name in the MEND statement is optional. n Macros will be easier to debug or remove errors if the macro-name is included in the MEND statement n Easier to match MACRO and MEND statements makes the sandwich complete
28
I OWA S TATE U NIVERSITY Department of Animal Science Macros u Macros can be stored in a central location called an autocall location n Can be used multiple times in different programs by a single programmer n Can be used by multiple programers if they have access to a centrally stored autocall location as long as they have access or permission n Easy way to store macros that might be useful multiple times by a single or multiple users
29
I OWA S TATE U NIVERSITY Department of Animal Science Macros u Macros can have multiple parameters n Same report but with a different set of data n Example l %Macro macro-name (parameter-1=, parameter-2, parameter-n= ); Macro-text; %MEND macro-name; Example: A macro named %QUATERLYREPORT might start like this; %Macro quarterlyreport (quarter=, salesrep= ); Could invoke Macro by the following statement; %quarterlyreport (quarter=3,salesrep=Smith);
30
I OWA S TATE U NIVERSITY Department of Animal Science Macros u Likely will not use macros a great deal with one exception u A macro called PDMIX n Separates means from a Proc Mixed analysis with a user defined separation technique l Duncans, Tukeys, etc. n Assigns letter names to separated means of one or more class variable defined in a Proc Mixed analysis
31
I OWA S TATE U NIVERSITY Department of Animal Science PDMIX macro PURPOSE: This macro takes two data sets from Proc MIXED (Version 8), created by the DIFFS option on the LSMEANS statement. If an ADJUST= option is used, the pdiffs from this are used, not the unadjusted defaults. The pdiffs are converted to groups, labeled by numbers, and this is merged onto the lsmeans data set. The numbers are converted to letters, and for cases where more than 26 letters are needed, sections of letters are coded. For example, 3 means might have the letters A, (2)A, and (3)A. These 3 means are all different, because although all have the letter A, each A belongs to a different section, identified by (#).
32
I OWA S TATE U NIVERSITY Department of Animal Science Example use of PDMIX Macro Example of use. Assume the file pdmix800.sas, containing the macro code, is on the a: drive. Then the code below will run MIXED, and run pdmix800 on the lsmeans. MIXED is told not to print the means and pdiffs, using the ODS exclude statement, as pdmix800 does the printing in the more desirable format. Also shown are two optional parameters.
33
I OWA S TATE U NIVERSITY Department of Animal Science Example use of PDMIX Macro Proc Mixed; class block a b; model y = a b a*b; random block; lsmeans a b a*b/pdiff; ods output diffs=ppp lsmeans=mmm; ods listing exclude diffs lsmeans; run; %include 'a:pdmix800.sas'; %pdmix800(ppp,mmm,alpha=.01,sort=yes);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.