Macro Code Development HRP 223 – 2012 Nov 12, 2012 Copyright © 2012 Raymond R. Balise. All rights reserved. Warning: This presentation is protected by copyright law and international treaties. Unauthorized reproduction of this presentation, or any portion of it, may result in severe civil and criminal penalties and will be prosecuted to maximum extent possible under the law. 1 Keyboard Macros vs. Macros • Keyboard/Editor macros • Macros – are programs – automate tasks – facilitate sharing solutions to complex problems 2 Graphic for Rare Diseases 3 Recycling is Efficient 4 Using a Macro in EG https://www.stanford.edu/~balise/CPIC/plotIt 5 Using a Macro in SAS https://www.stanford.edu/~balise/CPIC/plotItSAS 6 The definition of the plotit macro is here. This creates the macro but does not cause it to do anything. Expand the code if, and only if, you want to. Arguments are hopefully well named. They are a comma-delimited list. A modern macro will include comments like this for Enterprise Guide to display. %mend = macro end goes after the code. This calls/invokes the plotit macro. The macro is actually “done” when you include a line like this. 7 Macro Details • Macros begin with the word %macro and end with the word %mend. • As a user of the macro, you can ignore everything after the %macro line. • The first line typically has the parameters (aka arguments) that the macro needs. Hopefully the person who wrote the macro will give you the details on the arguments. – The parameters are filled in using the order you typed them unless the arguments have a name and = . 8 Making a Macro Replace the analysis variable with a macro variable. 9 Using a Global Macro Variable Add thingy to the global macro value lookup table. This is dangerous code because thingy will keep the value till you quit. Replace &thingy. with Weight. 10 Macros Happen First • The macro engine does the copy and paste to resolve the meaning of macro variables before the rest of the code is executed. 11 Macro Variables • Macro variable names are like SAS variable names but when you use them, precede the name with & and follow with a . • The final decimal point is optional but it is a very good idea to include it. 12 Using a Local Macro Variable Make a macro variable called thingy that will be destroyed when the macro is done. Replace &thingy. with weight on first call and with age on second call. 13 14 Position or Named Arguments • If you have defaults for the macro arguments, used a named list. • Positional arguments go first in a fixed order. • Named arguments can be in any order. They are called with the argument name and an = 15 Macro Statements • You have seen %macro %mend and %let statements. • You can write complex macro code with %if and %do 16 A new named argument with a default of YES If the plot macro variable is YES then include the plot. 17 18 Macros Calling Macros Hard coding these details makes no sense. Invoke the plotIt macro and pass it a few parameters. 19 20 %sysfunc says to do a regular SAS function. Here compress away the “ Pass a quoted string. 21 Other Examples • I want to make a quantile plot to show the percentiles for a dataset. 22 How to do this…. • Test to see if the dataset exists and if it exists, count the records. • Make a temporary dataset after doing some algebra. Then make the plot. Finally, remove the temporary dataset. 23 Counting Records Try to open a pipe to a file. If it failed, say 0 records. Otherwise get the record count attribute. Close the pipe. 24 Checking That it Worked It behaves with missing data. It returns the number of records. 25 Put the values into a string. Put those values into a macro variable during the run. 26 Use the values. 27 Reusing the Code • You can keep a library of useful code and include the snippets in your projects: %include "c:\sasMacros\quantPlot.sas"; • Or add to the autocall library: The folder has quantPlot.sas and nobsG.sas 28 Sensitivity, Specificity, Positive and Negative Predicted Value • Somehow SAS forgot them… In the log In the log 29 Binomial Probabilities • If you need to calculate binomial probabilities, look at my macro: Proc reliability does confidence intervals but it is expensive… 30 31 Macro Books Carpenter's Complete Guide to the SAS Macro Language, Second Edition By: Art Carpenter Only hard copy SAS Macro Programming Made Easy, Second Edition By: Michele M. Burlew proquest.safaribooksonline.com/book/databases/sas/9781590478820 32