handout_sas_09_SAS_Macro

11 sugi 29 tutorials sort by quantity proc sort data

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: it until a later step. Example The marketing department wants a report showing all the orders placed by their best customer. The following program finds the customer with the single largest order and then prints all the orders for that customer. 11 SUGI 29 Tutorials *Sort by Quantity; PROC SORT DATA = orders; BY DESCENDING Quantity; *Use CALL SYMPUT to find biggest order; DATA _NULL_; SET orders; IF _N_ = 1 THEN CALL SYMPUT("biggest", CustomerID); ELSE STOP; *Print all obs for customer with biggest order; PROC PRINT DATA = orders NOOBS; WHERE CustomerID = "&biggest"; FORMAT OrderDate DATE7.; TITLE "Customer &biggest Had the Single Largest Order"; RUN; This program has three steps. First PROC SORT sorts the data by descending Quantity so that the first observation is the largest single order. Then a DATA step reads the sorted data, and in the first iteration of the DATA step (when _N_ = 1), uses CALL SYMPUT to assign the current value of CustomerID (287) to the macro variable &BIGGEST. The keyword _NULL_ in the DATA statement tells SAS not to bother creating a new SAS data set, and the STOP statement tells SAS to stop after the first iteration. The DATA _NULL_ and STOP are not necessary but they make the program more efficient by preventing SAS from reading and writing observations that won't be used. The final step takes the macro variable &BIGGEST and inserts its value into the PROC PRINT so that SAS selects just the orders for customer number 287. Here are the standard SAS statements that SAS will see after the macro processor has resolved the final step: *Print all obs for customer with biggest order; PROC PRINT DATA = orders NOOBS; WHERE CustomerID = "287"; FORMAT OrderDate DATE7.; TITLE "Customer 287 Had the Single Largest Order"; RUN; The output will look like this: Customer 287 Had the Single Largest Order Customer ID 287 287 287 287 Order Date 21OCT03 21OCT03 15OCT03 15OCT03 Model Delta Santa Delta Santa Breeze Ana Breeze Ana Quantity 30 2...
View Full Document

This note was uploaded on 03/21/2013 for the course AMS 597 taught by Professor Xing during the Spring '11 term at SUNY Stony Brook.

Ask a homework question - tutors are online