Upload
nathaniel-bryan
View
215
Download
0
Embed Size (px)
Citation preview
• 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…
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.
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”;
• 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.
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...
• 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;%plotproc print; run; etc.....
• 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.
• Use the data (custID, sale date, variety sold, quantity)
240W 02-07-2003 Ginger 120240W 02-07-2003 Protea 180356W 02-08-2003 Heliconia 60356W 02-08-2003 Anthurium 300188R 02-11-2003 Ginger 24188R 02-11-2003 Anthurium 24240W 02-12-2003 Heliconia 48240W 02-12-2003 Protea 48356W 02-12-2003 Ginger 240
• 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;
• 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…