sas macro programming for beginners · change in your program and have sas echo that change...

21
SAS Macro Programming for Beginners

Upload: others

Post on 20-Jul-2020

21 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

SAS MacroProgramming for Beginners

𝜏𝜌

Page 2: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Why Use Macros?

• First, with macros you can make one smallchange in your program and have SAS echothat change throughout your program.

• Second, macros can allow you to write a pieceof code and use it over and over again in thesame program or in different programs

• Third, you can make your programs datadriven, letting SAS decide what to do based onactual data values.

Page 3: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Rules?

• Macro → the names of macros start with apercent sign (%)

• Macro variable → the names of macrovariables start with an ampersand (&)

Page 4: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Creating Modular Code with Macros

• The general form of a macro is

%MACRO macro-name;

macro-text

%MEND macro-name;

• Calling the macro%macro-name

Page 5: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Example 1

• A company that manufactures bicycles maintains a filelisting all their models. For each model they record itsname, class (Road, Track, or Mountain), list price, andframe material. Here is a subset of the data:

Black Bora Track 796 Aluminum

Delta Breeze Road 399 CroMoly

Jet Stream Track 1130 CroMoly

Mistral Road 1995 Carbon Comp

Nor'easter Mountain 899 Aluminum

Santa Ana Mountain 459 Aluminum

Scirocco Mountain 2256 Titanium

Trade Wind Road 759 Aluminum

Page 6: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Example 1

• Data preparationDATA models;

INFILE 'e:\Models.txt' TRUNCOVER;

INPUT Model $ 1-12 Class $ Price Frame $ 28-

38;

RUN;

Page 7: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Example 1

• the sales personnel like to have lists of models sorted both by model name and by price, create a macro for these purpose!

Page 8: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Answer 1

%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

Page 9: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Adding Parameters to Macros

• To add parameters to your macro, simply list the macro-variable names followed by an equal sign in parentheses after the macro name :

%MACRO macro-name (parameter-1=,

parameter-2=, . . . parameter-n=);

macro-text

%MEND macro-name;

Page 10: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Example 2

• The PRINTIT macro from the previous exampleprints the desired reports, but you have tosort the data between calls to the macro andthe title does not reflect the sort order of thereport, so create the macro!

Page 11: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Answer 2

%MACRO sortandprint(sortseq=, sortvar=);

PROC SORT DATA = models;

BY &sortseq &sortvar;

PROC PRINT DATA = models NOOBS;

TITLE 'Current Models';

TITLE2 "Sorted by &sortseq &sortvar";

VAR Model Class Frame Price;

FORMAT Price DOLLAR6.;

RUN;

%MEND sortandprint;

%sortandprint(sortseq=Descending, sortvar=Price)

%sortandprint(sortseq=, sortvar=Class)

Page 12: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Conditional Logic

• Here are the general forms of statements used for conditional logic in macros:

%IF condition %THEN action;

%ELSE %IF condition %THEN action;

%ELSE action;

%IF condition %THEN %DO;

action;

%END;

Page 13: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Example 3

• The company maintains a file with information about every orderthey receive. For each order, the data include the customer IDnumber, date the order was placed, model name, and quantityordered. Here are the data:

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

Page 14: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Example 3

• Data pereparationDATA orders;

INFILE 'e:\Orders.txt';

INPUT CustomerID $ 1-3 OrderDate DATE7.

Model $ 13-24 Quantity;

RUN;

Page 15: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Example 3

• Every Monday the president of the company wants adetail-level report showing all the current orders. OnFriday the president wants a report summarized bycustomer. You could write two separate programs torun these reports, or you could write one programusing macros.

Page 16: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Answer 3

%MACRO reports(sday=);

%IF &sday = Monday %THEN %DO;

PROC PRINT DATA = orders NOOBS;

FORMAT OrderDate DATE7.;

TITLE "&sday Report: Current Orders";

%END;

%ELSE %IF &sday = Friday %THEN %DO;

PROC TABULATE DATA = orders;

CLASS CustomerID;

VAR Quantity;

TABLE CustomerID ALL, Quantity;

TITLE "&sday Report: Summary of Orders";

%END;

%MEND reports;

RUN;

%reports(sday=Monday)

RUN;

%reports(sday=Friday)

RUN;

Page 17: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Example 4

• Create a macro for regression analysis with data below:Y X1 X2

3.929 1 1

5.308 2 4

7.239 3 9

9.638 4 16

12.866 5 25

17.069 6 36

23.191 7 49

31.443 8 64

Page 18: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Answer 4

• Data preparationDATA datareg;

INFILE 'e:\regdata.txt';

INPUT y x1 x2;

RUN;

Page 19: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Answer 4%macro regresi (finput=, foutput=);

proc iml;

use &finput; /* file data SAS dari SAS/BASE */

read all var{y} into Y;

read all var{x1 x2} into X;

nb = nrow(X);

nk = ncol(X);

satu = J(nb,1,1);

X = satu || X;

beta = inv(X`*X) * (X`*Y);

yduga = X * beta;

sisaan = y-yduga;

create &foutput var{beta sisaan yduga}; /* file data

SAS hasil ‘proc iml’ */

append;

quit;

%mend regresi;

Page 20: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Answer 4

%regresi (finput= datareg, foutput=hasil);

proc print data=hasil NOOBS;

var beta;

run;

proc gplot data=hasil;

plot sisaan*yduga;

run;

Page 21: SAS Macro Programming for Beginners · change in your program and have SAS echo that change throughout your program. ... •Macro →the names of macros start with a ... %macro-name

Thank you