sas macro language

14
SAS Macro Language

Upload: quinta

Post on 05-Jan-2016

60 views

Category:

Documents


0 download

DESCRIPTION

SAS Macro Language. Write and debug SAS program without macro coding. Generalize by replacing hard coded values with macro variable references. Create a macro definition with macro parameters. Add macro level programming for conditional and iterative processing. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: SAS Macro Language

SAS Macro Language

Page 2: SAS Macro Language

Write and debug SAS programwithout macro coding

Generalize by replacing hard coded values with macro variable references

Create a macro definition with macro parameters

Add macro level programming for conditional and iterative processing

Add data driven customization

Page 3: SAS Macro Language

Marketing Data

region city product sales revenue

Northeast New York sedan 400 100Northeast Boston coupe 800 200Midwest Chicago minivan 200 50Midwest Detroit suv 100 25Northwest Seattle sedan 600 150Northwest Boise coupe 20 5Southeast Raleigh minivan 200 50Southeast Atlanta suv 400 100Southwest Dallas sedan 1000 250Southwest Houston coupe 2000 500

Page 4: SAS Macro Language

proc sort data=lib1.marketing (where=(region="Northeast")) out=sales; by city product;run;

title1 "Sales Report for Northeast Region";

proc print data=sales noobs;var city product sales revenue; run;

Write and debug SAS programwithout macro coding

Page 5: SAS Macro Language

Sales Report for Northeast Region

city product sales revenue

Boston coupe 800 200New York sedan 400 100

Write and debug SAS programwithout macro coding

Page 6: SAS Macro Language

%let region = Northeast; %let sortvar = city product;

proc sort data=lib1.marketing (where=(region="&region")) out=sales;by &sortvar;run;

title1 "Sales Report for &region Region";

proc print data=sales noobs;var &sortvar sales revenue;run;

%put region=&region sortvar=&sortvar;

Generalize by replacing hard coded values with macro variable references

Page 7: SAS Macro Language

Sales Report for Northeast Region

city product sales revenue

Boston coupe 800 200New York sedan 400 100

Generalize by replacing hard coded values with macro variable references

Page 8: SAS Macro Language

%macro sales (region=, sortvar=city product);

proc sort data=lib1.marketing (where=(region="&region")) out=sales;by &sortvar;run;

title1 "Sales Report for &region Region";proc print data=sales noobs;var &sortvar sales revenue; run;

%put region=&region sortvar=&sortvar;

%mend sales;

%sales (region=Northeast);

Create a macro definition with macro parameters

Page 9: SAS Macro Language

Sales Report for Northeast Region

city product sales revenue

Boston coupe 800 200New York sedan 400 100

Create a macro definition with macro parameters

Page 10: SAS Macro Language

%macro sales (region=, sortvar=city product) / minoperator;

%let reglist = Northeast Midwest Northwest Southeast Southwest;

%if &region in &reglist %then %do;

proc sort data=lib1.marketing (where=(region="&region")) out=sales; by &sortvar;run;title1 "Sales Report for &region Region";proc print data = sales noobs;var &sortvar sales revenue; run;%end;%else %put Invalid value for region=&region;%mend sales;%sales (region=Northeast);

Add macro level programming for conditional and iterative processing

Page 11: SAS Macro Language

Sales Report for Northeast Region

city product sales revenue

Boston coupe 800 200New York sedan 400 100

Add macro level programming for conditional and iterative processing

Page 12: SAS Macro Language

%macro sales (region=,sortvar=city product) / minoperator;proc sql noprint;select distinct regioninto :reglist separated by " "from lib1.marketing;quit;%put reglist=&reglist;%if &region in &reglist %then %do; proc sort data=lib1.marketing (where=(region="&region")) out=sales; by &sortvar;title1 "Sales Report for &region Region";proc print data = sales noobs;var &sortvar sales revenue; run;%end;%else %put No customers on file from region=&region;%mend sales;%sales (region=Northeast);

Add data driven customization

Page 13: SAS Macro Language

Sales Report for Northeast Region

city product sales revenue

Boston coupe 800 200New York sedan 400 100

Add data driven customization

Page 14: SAS Macro Language

Questions?