Download presentation
Presentation is loading. Please wait.
Published byNathaniel Bryan Modified over 8 years ago
1
FOR MONDAY: Be prepared to hand in a one-page summary of the data you are going to use for your project and your questions to be addressed in the project Read Chapter 7 (7.1-7.5) on Macro-Programming: –macros allow you to make small changes in code that ripple throughout a long program –macros allow you to write a piece of code once and reuse it many times over… –macros allow you to make you code data dependent - SAS decides what to do based on the values of the data. Practice macro writing on the project data…
2
Macros and macro variables The macro processor “resolves” your macro code so it can generate legitimate SAS code. SAS macro code consists of macros and macro variables. Macro variables are prefaced with an ampersand (&) and macros are prefaced with a percent sign (%). A macro can be a complex piece of code with DATA steps, PROC steps and macro statements like %DO or %LET, etc. A macro variable has only one value, always a character.
3
Macro variables Create a macro variable by using the %LET statement. This is an efficient way to replace text strings in SAS... %LET macro-variable-name = text-value ; Then to use that particular text string in a SAS program refer to the macro variable with an ampersand (&). Note quotes not required around the “text-value”. e.g., %LET X = freshman; title “Data analysis for the &X Class”;
4
NOTE the difference between a global and a local macro variable: %LET outside a macro definition defines a global variable. Otherwise, the macro variable is local. Try the example program on page 203 - note that the code only has to be changed in one place in the program.
5
Macro definitions Macros allow you to substitute text of SAS code within a SAS program. –Each macro has a name and is defined between the %MACRO and the %MEND statements... E.g., %MACRO macro-name; macro-definition %MEND macro-name; –To invoke the macro definition you defined as above, use the statement %macro-name within your SAS program and the text in the macro-definition will be executed...
6
See the example in Section 7.3 on p. 205. or this example… %macro plot; proc plot; plot totmass*plantht; run; %mend plot; Later you may use this macro as: data temp; set save.padgett; if marsh=“ph” ; run; %plot proc print; run; etc.....
7
The above macros would be much more useful if we could pick the variables we want to plot... this requires the use of parameters within the macro. E.g.: %macro plot(yvar=,xvar= ); proc plot; plot &yvar*&xvar; run; %mend plot; Then invoke the macro by referring to it and also inserting the specific variables you want to plot: %plot(yvar=totmass, xvar=plantht) Do the example on page 207 - the macro %SELECT is created with 2 parameters creating 2 macro variables &CUSTOMER and &SORTVAR.
8
Use the data (custID, sale date, variety sold, quantity) 240W 02-07-2003 Ginger 120 240W 02-07-2003 Protea 180 356W 02-08-2003 Heliconia 60 356W 02-08-2003 Anthurium 300 188R 02-11-2003 Ginger 24 188R 02-11-2003 Anthurium 24 240W 02-12-2003 Heliconia 48 240W 02-12-2003 Protea 48 356W 02-12-2003 Ginger 240
9
We may use conditional logic in macros to write different code depending upon the value of certain macro variables for example: %MACRO dailyreports; %IF &SYSDAY = Monday %THEN %DO; PROC PRINT DATA = flowersales; FORMAT SaleDate WORDDATE18.; TITLE 'Monday Report: Current Flower Sales'; %END; %ELSE %IF &SYSDAY = Tuesday %THEN %DO; PROC MEANS DATA = flowersales MEAN MIN MAX; CLASS Variety; VAR Quantity; TITLE 'Tuesday Report: Summary of Flower Sales'; %END; %MEND dailyreports; DATA flowersales; INFILE 'c:\MyRawData\TropicalSales.dat'; INPUT CustomerID $ @6 SaleDate MMDDYY10. @17 Variety $9. Quantity; RUN; %dailyreports RUN;
10
Note that SAS creates automatic macro variables that you may use in your programs: &SYSDATE is the character value of the date that the session began &SYSDAY is the character value of the day of the week that the session began… end chapter 7… next time we’ll do graphics…
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.