Академический Документы
Профессиональный Документы
Культура Документы
BY:
GURBEER SINGH
Objectives
Macro Variable
SAS macro variables enable you to substitute text in your
SAS programs. Macro variables can supply a variety of
information, including
operating system information
SAS session information
text strings
SAS macro variables make your programs more reusable and
dynamic
The macro variables can be defined & used anywhere in SAS
programs, except within data lines
Example:
%let dsname=Newdata;
Where DSNAME is the name of the macro variable.
Newdata is the value of the macro variable dsname.
1.
%let sto=24;
2.
%LOCAL statement
%GLOBAL statement
%MACRO statement
SYMPUT routine
INTO clause
10
Example:
%let a=b;
%let b=c;
%let c=10;
1.&a;
2.&&a;
3.&&&a;
4.&&&&a;
5.&&&&&a;
6.&a&b;
7.&&a&b;
8.&&a.&b;
11
Output:
1.
2.
3.
4.
5.
6.
b
b
c
b
c
bc
8. bc
12
Question:
The following SAS program is submitted:
%let dept=prod;
%let prod=merchandise;
The following message is written to the SAS log:
The value is "merchandise"
Which SAS System option writes this message to the SAS log?
A. %put the value is "&&&dept";
B. %put the value is "&&dept";
C. %put the value is "&dept";
D. %put the value is &&&&dept;
ANSWER:
13
14
Example:
%let mon=JAN;
%let yr=2012;
%put &mon , &yr;
This writes in the SAS log:
JAN , 2012
15
16
A Macro Problem
Problem: You reference a SAS datasetname several times in a SAS
job.
DATA PAYROLL;
INPUT EMP$ RATE;
DATALINES;
TOM 10
JIM 10
;
PROC PRINT DATA=PAYROLL;
TITLE "PRINT OF DATASET PAYROLL";
RUN;
Question: How can you change the name quickly in one place only AND
have the datasetname appear in a title?
17
18
Notes:
Macro variables are not resolved within single quotes.
19
Defining a Macro
A macro or macro definition enables you to write macro
programs.
General form of a macro definition:
%MACRO
%MACROmacro-name;
macro-name;
macro-text
macro-text
%MEND
%MEND<macro-name>;
<macro-name>;
Macro Compilation
When a macro definition is submitted,
macro language statements are
checked for syntax errors
compiled
SAS statements and other text are not
checked for syntax errors
compiled
the macro is stored as an entry in a SAS catalog, the
temporary catalog work.sasmacr by default.
21
SAS/Macros
(Passing Parameters)
Passing Information into a Macro Using Parameters
A macro variable defined in parentheses in a %MACRO
statement is a macro parameter. Macro parameters enable
you to pass information into a macro.
%macro plot(yvar= ,xvar= );
proc plot data=test;
plot &yvar*&xvar;
run;
%mend plot;
You invoke the macro by providing values for the parameters,
as follows:
%plot(yvar=income,xvar=age)
22
Macro Parameters
General form of a macro call with parameters:
%macro-name(value-1,
%macro-name(value-1,
value-n)
value-n)
23
24
25
26
27
VALUE
SYSDATE
SYSDAY
SYSTIME
SYSSCP
SYSVER
SYSERR
SYSLAST
28
Practice Exercise:
Exercise 1 :
Determine the values of the following system macro variables using your
current computer system.
&SYSDAY
Current day of the week
&SYSTIME
Current time
&SYSSCP
Operating system being used
&SYSVER
Current SAS version number
&SYSDATE
Current date, in DATE7. Format
Exercise 2:
Using system macro variables run a PROC CONTENTS and a PROC
PRINT on the LAST SAS dataset that was created. Include its name in a
title.
29
Exercise 1 Solution
%PUT **** SYSDAY = &SYSDAY;
%PUT **** SYSTIME = &SYSTIME;
%PUT **** SYSSCP = &SYSSCP;
%PUT **** SYSVER = &SYSVER;
%PUT **** SYSDATE = &SYSDATE;
30
Exercise 2 Solution:
proc contents data=&syslast;
title "contents of &syslast";
run;
The Generated Code:
proc contents data=WORK.COUNTYDT;
title "contents of WORK.COUNTYDT";
run;
Proc print data=&SYSLAST;
Run;
31
Question:
The following SAS program is submitted:
%macro execute;
<insert statement here>
proc print data = sasuser.houses;
run;
%end;
%mend;
Which of the following completes the above program so that it executes on Tuesday?
A. %if &sysday = Tuesday %then %do;
B. %if &sysday = 'Tuesday' %then %do;
C. %if "&sysday" = Tuesday %then %do;
D. %if '&sysday' = 'Tuesday' %then %do;
Answer:
32
Quick Quiz
Does a %LET statement outside of a macro program
create a macro variable in the global or local symbol
table?
33
34
Example:
%macro holinfo(day,date);
%let holiday=Christmas;
%put *** Inside macro: ***;
%put *** &holiday occurs on &day, &date. ***;
%mend holinfo;
%holinfo(Thursday,12/25/2011)
%put *** Outside macro: ***;
%put *** &holiday occurs on &day, &date. ***;
35
Output:
*** Inside macro: ***
*** Christmas occurs on Thursday, 12/25/2011 ***
*** Outside macro: ***
WARNING: Apparent symbolic reference HOLIDAY not resolved.
WARNING: Apparent symbolic reference DAY not resolved.
WARNING: Apparent symbolic reference DATE not resolved.
*** &holiday occurs on &day, &date. ***
36
Question:
The following SAS program is submitted:
%let a=cat;
%macro animal(a=frog);
%let a=bird;
%mend;
%animal(a=pig)
%put a is &a;
What is written to the SAS log?
A. a is pig
B. a is cat
C. a is frog
D. a is bird
Answer: B
37
CONDITIONAL LOGIC
%IF condition %THEN action;
%ELSE %IF condition %THEN action;
%ELSE action;
%IF condition %THEN %DO;
action;
%END;
38
39
40
41
42
Practice Exercises
Exercise :
If the following macro was defined to the SAS system:
%MACRO FREQ(DSN,VAR1,VAR2);
PROC FREQ DATA=&DSN;
TABLES &VAR1*&VAR2 / NOPERCENT;
RUN;
%MEND FREQ;
What code would the SAS compiler see after this macro call?
%FREQ(FREQ,SALARIES,SALES)
43
Exercise Solution
PROC FREQ DATA=FREQ;
TABLES SALARIES*SALES / NOPERCENT;
RUN;
44
45
a=%eval(1+2);
b=%eval(10*3);
c=%eval(4/2);
i=%eval(5/3);
Question:
The following SAS program is submitted:
%let value = .5;
%let add = 5;
%let newval = %eval(&value + &add);
Which one of the following is the resulting value of the macro variable
NEWVAL?
A. 5
B. 5.5
C. .5 + 5
D. Null
46
Answer: D
47
48
49
Exercise:
Write any small macro program after setting all the three
options and check the log for results.
50
Call SYMPUT
CALL SYMPUT takes a value from a DATA step and
assigns it to a macro variable which you can then use later in your
program
Syntax:
CALL SYMPUT(macro-variable, value);
51
Example:
data _null_;
format var datetime.;
var=datetime();
call symput('date_time',var);
run;
%Put DateTime- &date_time;
52
Example:
The data corresponds to : Emergency room visits with doctor
names and charged amounts.
data ervisits;
input doctor $ charged;
datalines;
White 358
Smith 935
White 421
Jones 144
Smith 105
Jones 1234
;
run;
53
Question:
How we can get name of the First Doctor that visited the
Emergency Room into Macro Variable?
54
Solution:
data _null_;
set ervisits;
if _n_ = 1 then
call symput('frstdoc',doctor);
run;
%put First ER Visit on Record was treated by &frstdoc;
Log
The First ER Visit on Record was treated by White
55
56
57
58
59
Question:
60
Question Contd
%do i=1 %to &numgrp;
proc print data =sasuser.highway;
where status ="&&group&I";
run;
%end;
%mend;
%highway
How many reports are produced?
A. 2
B. 6
C. 0
D. 5
61
Answer: A
62
Question:
The following SAS program is submitted:
options yearcutoff = 1950;
%macro y2kopt(date);
%if &date >= 14610 %then %do;
options yearcutoff = 2000;
%end;
%else %do;
options yearcutoff = 1900;
%end;
%mend;
63
Question Contd
data _null_ ;
date = "01jan2000"d;
call symput("date",left(date));
run;
%y2kopt(&date)
The SAS date for January 1, 2000 is 14610 and the SAS system option for
YEARCUTOFF is set to 1920 prior to submitting the above program. Which one of
the following is the value of YEARCUTOFF when the macro finishes execution?
A. 1900
B. 1920
C. 1950
D. 2000
Answer: D
64
library
2. %Include statement
65
66
% INCLUDE statement
Users can also use %INCLUDUE to include a macro stored in an
external file. Shown below are the syntax and sample code:
%INCLUDE source;
Eg.
%include 'c:\maclib\flag.sas' ;
67
Draft
Output
Delivery
System (ODS)
1. Integrate
your output.
Benefits
2. Customize content of your output.
3. Customize appearance of your output.
4. Improve your customer satisfaction
68
Draft
69
Draft
70
Draft
If you dont like this look, you can change it by switching styles. The table above uses the default style, which is called Default.. For a different look try:
71
Draft
72
Draft
Embedded Images
73
Draft
Embedded Links
74
Questions?
75