SAS Macro Exercises Introduction to SAS Macro functions SAS Macro functions are basically like subroutines in other languages, in that they let you run many SAS statements easily by just providing a few key pieces of information. In this course we will not focus on writing your own Macro functions (come see me at office hours if you want to learn more about this), instead we will focus on learning how to use the Macros that are included by the author of our text. To use these Macro functions we just need to have SAS read the Macro functions, and then we have to know what arguments to provide to each Macro function. Here is one of our first examples of the use of Macros, from the webpage http://www.webpages.uidaho.edu/~chrisw/stat422/carssample2.sas: ----------------------------------------------------------------------------------------------- options ls = 80 nodate formdlim= '-' ; %include 'd:\srs' ; %include 'd:\est_srs' ; data cars93 ; length manufac $ 13 model $ 10 ; infile 'd:\cars93.txt' firstobs = 2 ; input MANUFAC $ MODEL $ TYPE $ MINPRICE MIDPRICE MAXPRICE MPGCITY MPGHIGH AIRBAGS DRIVETR CYLINDR LITERS HPOWER RPMMAX US TYPECODE ROW ; run ; proc print ; run ; proc means ; run ; %srs(frame=cars93,npop=92,n=10,sample=cars93_srs,seed=21934) ; proc print data = cars93_srs ; run ; %est_srs(sample=cars93_srs,npop=92,response=mpgcity,param=mean) ; ----------------------------------------------------------------------------------------------- The four lines that involve Macros all begin with a '%' sign. %include: The %include statement tells SAS to read a file, in this case it tells SAS where to read two Macros called SRS and EST_SRS. Having the %include statement in this program achieves the same result as if we had pasted the SAS code in the files 'srs.' and 'est_srs.' directly into the SAS program. Its purpose is just to make the SAS program more readable, as we do when we use an INFILE statement instead of reading data directly with the CARDS statement. To use the Macros %srs and %est_srs, you will need to copy the Macro code (available on your CD-Rom with the text or from my website) to a file on your computer, then read them in with the %include statement. The trickiest part of using these %include statements is that if you save these file from a text editor like Notepad, it will try to automatically add a '.txt' ending to the filename. This can create a problem if you reference a file with %include 'c:\temp\srs' ; but the file in your c:\temp directory is instead called 'srs.txt'. Just make sure of what your filename is with Windows Explorer or the DOS DIR command so that SAS can find where you have saved the Macro file. %srs(frame= ,npop= ,n= ,sample= ,seed= ) ; The %srs Macro takes a simple random sample from a SAS data set and places it into a different SAS data set. The arguments are FRAME (the original SAS data set), NPOP (the population size), N (the sample size to draw), SAMPLE (the name of the SAS data set to be created for the sample), and SEED (a number that is used to obtain repeatable random samples). The call to %srs in the program: %srs(frame=cars93,npop=92,n=10,sample=cars93_srs,seed=21934) ; takes the data in the cars93 SAS data set, which contains 92 observations, and draws a simple random sample of size 10 and places those 10 observations (and all of their variables) into the SAS data set cars93_srs. The set of random numbers is initialized by the seed value 21934 so that if this program is run again, the same sample will be drawn. %est_srs(sample= ,npop= ,response= ,param= ) ; The %est_srs Macro calculates parameter estimates and error bounds for data from a simple random sample. The arguments are SAMPLE (the name of the SAS data set that contains the simple random sample), NPOP (the population size that the SAMPLE data set was drawn from), RESPONSE (the name of the variable of interest), and PARAM (the parameter to be estimated, either mean, total, or proportion). In the program above, the call to %est_srs is: %est_srs(sample=cars93_srs,npop=92,response=mpgcity,param=mean) ; takes the data from the SAS data set cars93_srs, which came from a population of size 92, and calculates an estimate of the population mean and an error bound for the MPGCITY variable. Macro exercise: 1) Run the 'TV StRS' example from lecture 11. 2) Write a SAS program to do homework problems 5.6a and 5.6b.