handout_sas_09_SAS_Macro

Macro name is a name you make up for your macro the

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: AS that this is the beginning of the macro and the %MEND statement signals the end of the macro. Macro-name is a name you make up for your macro. The name must follow standard SAS naming conventions (start with a letter or underscore; contain only letters, numerals or underscores; and can be up to 32 characters in l ength). You don't need to specify the macro-name in the %MEND statement, but it will make your programs easier to understand if you include it. Macro-text represents the SAS statements that you want in your macro. After you define your macro, you need to invoke it when you want to use it. Do this by adding a percent sign in front of the macro-name like this: %macro-name While you do not need to end this statement with a semicolon, it generally does no harm either. Example Looking again at the bicycle models data, the sales personnel like to have lists of models sorted both by model name and by price. The following program creates a macro named PRINTIT that has the PROC PRINT statements you need for the report. Then the program calls the macro twice; first without sorting the data (it is already sorted by model name), and then after executing a PROC SORT by Price. %MACRO printit; PROC PRINT DATA = models NOOBS; TITLE 'Current Models'; VAR Model Class Frame Price; FORMAT Price DOLLAR6.; RUN; %MEND printit; %printit PROC SORT DATA = models; BY Price; %printit Here are the standard SAS statements that are generated by the macro processor. The first PROC PRINT is generated by the first call to the PRINTIT macro, the PROC SORT comes from the original program and the second PROC PRINT is generated by the second call to the PRINTIT macro. PROC PRINT DATA = models NOOBS; TITLE 'Current Models'; VAR Model Class Frame Price; FORMAT Price DOLLAR6.; RUN; PROC SORT DATA = models; BY Price; PROC PRINT DATA = models NOOBS; TITLE 'Current Models'; VAR Model Class Frame Price; FORMAT Price DOLLAR6.; RUN; 4 SUGI 29 Tutorials And here are the results of the two PROC PRINTs: one before sorting and one after sorting by Price. Current Models Model Black Bora Delta Breeze Jet Stream Mistral Nor'easter Santa Ana Scirocco Trade Wind Class Track Road Track Road Mountain Mountain Mountain Road Frame Aluminum CroMoly CroMoly Carbon Comp Aluminum Aluminum Titanium Aluminum Price $796 $399 $1,130 $1,995 $899 $459 $2,256 $759 Current Models Model Delta Breeze Santa Ana Trade Wind Black Bora Nor'easter Jet Stream Mistral Scirocco Class Road Mountain Road Track Mountai...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online