D. Call the Macro Again
THE MACRO PROCESSOR
Folio 1. You can refer to page 339 of our textbook for an example of a program that SAS would demand to pre-process earlier executing the lawmaking. In that particular case, earlier executing the code, SAS would accept to run the program through the macro processor in order to replace all the occurrences of "&NUMBER" with the value "v".
SUBSTITUTING TEXT WITH %LET
Page three. In case you want to try the program out on your own, here it is:
OPTIONS PS = 58 LS = 72 NODATE NONUMBER; Information models; infile DATALINES truncover; input Model $ 1-12 Class $ Price Frame$ 28-38; DATALINES; Black Bora Track 796 Aluminum Delta Breeze Road 399 CroMoly Jet Stream Rails 1130 CroMoly Mistral Route 1995 Carbon Comp Nor'easter Mountain 899 Aluminum Santa Ana Mount 459 Aluminum Scirocco Mountain 2256 Titanium Merchandise Current of air Road 759 Aluminum ; RUN; %LET bikeclass = Mountain; *Use a macro variable to subset; PROC PRINT Information = models NOOBS; where Course = "&bikeclass"; format Price dollar6.; championship "Current Models of &bikeclass Bicycles"; RUN;
You might also want to alter the value of Mountain in the %Allow argument to, say, Road, and re-run the program to encounter the effect.
CREATING MODULAR Lawmaking WITH MACROS
Page 4. Again, in example you lot want to effort the program out on your ain, here information technology is:
%MACRO printit; PROC PRINT information = models NOOBS; title 'Current Models'; var Model Course Frame Price; format Price dollar6.; RUN; %MEND printit; %printit PROC SORT data = models; by Price; RUN; %printit
Adding PARAMETERS TO MACROS
Page 6. Here's the code for the example:
%MACRO sortandprint(sortseq=, sortvar=); PROC SORT data = models; past &sortseq &sortvar; RUN; PROC PRINT data = models noobs; title 'Electric current Models'; title2 "Sorted past &sortseq &sortvar"; var Model Class Frame Price; format price dollar6.; RUN; %MEND sortandprint; %sortandprint(sortseq=Descending, sortvar=Price) %sortandprint(sortseq=, sortvar=Form)
Page 7. Hither's the code for the example with the MPRINT option invoked:
OPTIONS MPRINT; %MACRO sortandprint(sortseq=, sortvar=); PROC SORT information = models; by &sortseq &sortvar; RUN; PROC Impress data = models noobs; championship 'Electric current Models'; title2 "Sorted by &sortseq &sortvar"; var Model Class Frame Cost; format price dollar6.; RUN; %MEND sortandprint; %sortandprint(sortseq=Descending, sortvar=Price) %sortandprint(sortseq=, sortvar=Class)
CONDITIONAL LOGIC
Page 9. Here'due south the program:
Information orders; input CustomerID $ 1-3 @5 OrderDate date7. Model $ thirteen-24 Quantity; DATALINES; 287 15OCT03 Delta Breeze 15 287 15OCT03 Santa Ana 15 274 16OCT03 Jet Stream 1 174 17OCT03 Santa Ana 20 174 17OCT03 Nor'Easter 5 174 17OCT03 Scirocco 1 347 18OCT03 Mistral 1 287 21OCT03 Delta Breeze 30 287 21OCT03 Santa Ana 25 ; RUN; %MACRO reports; %IF &SYSDAY = Mon %And so %Do; PROC PRINT data = orders NOOBS; format OrderDate date7.; title "&SYSDAY Report: Current Orders"; RUN; %End; %ELSE %IF &SYSDAY = Fri %And so %DO; PROC TABULATE data = orders; class CustomerID; var Quantity; table CustomerID ALL, Quantity; title "&SYSDAY Report: Summary of Orders"; RUN; %Cease; %MEND reports; %REPORTS
Annotation! Considering the way the code is written, that if you don't run this program on a Monday or Friday, yous volition not go either of the pieces of output displayed in the paper. Only alter either the Monday or the Fri in the code to the day that you are running the code, and you'll generate some output.
By the fashion, you might notice that I embedded the RUN; statements in the macro, rather than placing them outside of the macro as the authors of the newspaper did. Either approach works... it's just a matter of preference.
DATA-DRIVEN PROGRAMS
Folio 12. Here's the program:
*Sort by Quantity; PROC SORT data = orders; by descending Quantity; RUN; *Use Phone call SYMPUT to find the biggest order; Information _NULL_; set up orders; if _N_ = 1 so Call SYMPUT("biggest", CustomerID); else End; RUN; *Print all obs for client with biggest gild; PROC PRINT data = orders NOOBS; where CustomerID = "&biggest"; format OrderDate date7.; title "Customer &biggest Had the Single Largest Order"; RUN
Source: https://online.stat.psu.edu/stat482/book/export/html/655
0 Response to "D. Call the Macro Again"
Postar um comentário