You are on page 1of 54

Introduction to SAS Essentials

Mastering SAS for Data Analytics

Alan Elliott and Wayne Woodward


1 SAS ESSENTIALS -- Elliott & Woodward
Chapter 7: Advanced Programming Topics
Part 2

2 SAS ESSENTIALS -- Elliott & Woodward


LEARNING OBJECTIVES
 To be able to create and use arrays
 To be able to use DO loops
 To be able to use the RETAIN statement
 To be able to create and use SAS® macro variables
 To be able to create and use SAS macro routines

3 SAS ESSENTIALS -- Elliott & Woodward


7.1 USING SAS ARRAYS
 A SAS ARRAY statement provides a method for placing a
series of observations into a single variable called an
array. Often these variables are referenced as part of a
DO loop, which is a technique for iterating through a set
of data. (DO loops are presented in the following section
of this chapter.)
 A simplified syntax for SAS arrays is the following:

ARRAY ARRAY-NAME(SUBSCRIPT) <$> ARRAY-VALUES;

4 SAS ESSENTIALS -- Elliott & Woodward


One-Dimensional Arrays
 One-dimensional arrays are specified with an ARRAY
statement, such as
ARRAY TIME(1:6) TIME1 - TIME6;
 This ARRAY statement creates a ID array called TIME with
an index value indicated in a parenthesis so TIME (1)
=TIME1, TIME (2) =TIME2, and so on. Six values
are assigned to the values TIME (1) through TIME (6) .
 Another way to specify the same array as above is

ARRAY TIME(6) TIME1 - TIME6;

5 SAS ESSENTIALS -- Elliott & Woodward


Specify Beginning and Ending Index Value
 Consider this array:

ARRAY TIME(0:5) A B C D E F;

 In this case TIME ( 0) =A, TIME ( 1) =B, and so on. Note


that the index begins with a 0 instead of 1, because the
initial definition of the array size in the parentheses (0:5)
specified a beginning and end for the index values.

6 SAS ESSENTIALS -- Elliott & Woodward


Unknown number of elements in an array
 Consider this array:

ARRAY ANSWER(*) Q1–Q5O;

 In this case, ANSWER (1) =Q1, ANSWER (2)=Q2,


and so on. The size of the array is not specified. Instead it
is indicated as (*). SAS figures out how many items are
being assigned to the array and makes it the required
size.

7 SAS ESSENTIALS -- Elliott & Woodward


Specify an array of text values
 Consider this array that specifies an array of text
(Character) values

ARRAY ANSWER(1:5) $ Q1–Q5;

 where Q1- QS are text values. Text arrays are created in


the same way as numeric arrays but with text values. The
$ after the array definition indicates to SAS that the array
is of character type.

8 SAS ESSENTIALS -- Elliott & Woodward


Specify Actual Values for a Created Array
 Consider this array:

 ARRAY VALUE[5] (2 4 6 8 10) ;

 This array specifies actual values for the array where


VALUE ( 1) =2, VALUE (2) =4, and so on. Note the
difference here is that the values are within parentheses
(you can use [], {},or() for brackets in the ARRAY
statement); similarly, the expression

9 SAS ESSENTIALS -- Elliott & Woodward


Specify actual character values for an array
Consider this array:

ARRAY FRUIT[3] $10 ("apple" "orange"


"lemon");

 Specifies actual character values of the FRUIT array as


text values. The $10 specifies that the items in the array
are character, and that the maximum number of
characters (length) for each item in the array is 10.

10 SAS ESSENTIALS -- Elliott & Woodward


Specify no values in an array
 Consider this array

ARRAY ABC[5];

 When no values or variables are given, SAS creates the


variables based on the array name: ABC1, ABC2, and so
on, and their initial values are missing values.

11 SAS ESSENTIALS -- Elliott & Woodward


Specify initial values for items in an array
 Consider this array:

ARRAY DRINKSIZE[*] SMALL REGULAR LARGE MEGA


(8, 16, 24, 32 );
 Or

ARRAY CUPSIZE[*] CUP1-CUP4 (8, 16, 24, 32);

 Specifies initial values for the items in an array. In the first example,
 DRINKSIZE ( 1 ) = SMALL = 8
 In the second example,
 CUPSIZE ( 1 ) = CUP1 - 8

12 SAS ESSENTIALS -- Elliott & Woodward


LBOUND, HBOUND, and DIM()
 When using (*) in a array specification, as in

ARRAY TIME(*) TIME1-TIME6;

 you may not know the value of the largest index. You can
access these values using SAS functions. In this case, the
upper bound is set to the value DIM (arrayname) or DIM
(TIME), which is 6. Moreover, the function LBOUND
(arrayname) represents the lowest index in the specified
array and HBOUND (arrayname) represents the highest
 index.
13 SAS ESSENTIALS -- Elliott & Woodward
Referring to Values in an Array
 Once an array is created (such as TIME [ 1 : 6] ), you can
refer to a value in the array by specifying an index value,
and use that value in any common SAS statement. For
example,

NEWVAL=(TIME[5]/365);

 This expression uses the value of TIME at index 5 in the


calculation.

14 SAS ESSENTIALS -- Elliott & Woodward


Hands on Example p 165 (DARRAY1.SAS)
DATA A;
INPUT ONE TWO THREE FOUR;
ARRAY T(4) one two three four;
TSUM1=SUM(of one two three four);
TSUM2=SUM(of T(1) T(2) t(3) T(4));
TSUM3=SUM(of T(*));
DATALINES;
1 2 3 4
5 6 7 8
;
RUN;
proc print data=a;run;
Do the exercises describe on page 166.
15 SAS ESSENTIALS -- Elliott & Woodward
Searching an Array
 There are a number of tasks that are made easier when
using arrays. One example is searching the contents of an
array for a specific number or text value. This is
accomplished with an IN statement. For example, the
statement

IF 3 IN MYARRAY THEN some expression;

 performs the expression when there is a 3 somewhere in


the array named MYARRAY.
 Do the Hands on Example p 166.
16 SAS ESSENTIALS -- Elliott & Woodward
Searching for a Value in an Array
 Find the value “ORANGE” in an Array
DATA B;
FORMAT ONE TWO THREE FOUR $10.;
INPUT ONE $ TWO $ THREE $ FOUR $;
ARRAY FRUIT(4) ONE TWO THREE FOUR;
IF "ORANGE" IN FRUIT then
ISORANGE=1;ELSE ISORANGE=0;
DATALINES;
APPLE ORANGE PINEAPPLE APRICOT
LEMON APPLE KIWI STRAWBERRY
;

17 SAS ESSENTIALS -- Elliott & Woodward


7.2 USING DO LOOPS
 The DO statement is a way to control the flow of your
program. These are the varieties of DO loops available in
SAS:
1. IF and DO: Use a DO in an IF statement to cause a group of
statements to be conditionally executed.
2. Iterative DO loop: Iterate through a series of statements
based on some incremental index.
3. DO UNTIL: Execute a group of statements until some
condition is met.
4. DO WHILE: Execute a group of statements as long as
specified condition is true.
 Note: The IF and DO combination statement was
discussed in Chapter 4.

18 SAS ESSENTIALS -- Elliott & Woodward


Iterative DO LOOP
 An iterative DO loop is a way of incrementing through series of
statements based on some index. A simplified syntax of an
iterative DO loop is as follows:
DO indexvariable= initialvalue to endvalue
<by incrementvalue>;
SAS statements . . .
END;
The INDEXVALUE is named
 For example,
ILOOP.
SUM=0;
DO ILOOP=1 to 4;
SUM=SUM + ILOOP;
ILOOP “iterates” from a starting
END; value of 1 to and ending value of 4

19 SAS ESSENTIALS -- Elliott & Woodward


Follow the Iterations of the Loop
 Follow the code: Since initially,
SUM=0 and the first value of
ILOOP=1 then the first time
SUM=0; through

DO ILOOP=l to 4; SUM=0 + 1;
 Subsequent loops are:

SUM=SUM+ILOOP; SUM=1+2;
SUM=3+3;
SUM=6+4;
END;  So,the final value of SUM at
the end of the loop is 10.

20 SAS ESSENTIALS -- Elliott & Woodward


A DO UNTIL Loop
 In a DO UNTIL loop, the increment is replaced with an
UNTIL clause with (ILOOP GT 4) in parentheses. For
example, This code produces the same
ILOOP=l; loop as the previous example,
using DO UNTIL instead of the
SUM=0; Iterative Do Loop

DO UNTIL (ILOOP GT 4);


SUM=SUM+ILOOP;
ILOOP+l;
END;
 Do Hands on Example p 169 (DWHILE.SAS)

21 SAS ESSENTIALS -- Elliott & Woodward


A DO WHILE Loop
 In DO WHILE, the loop continues until the condition is no
longer true. Once it is false, the loop stops. For example,
the following code results in the same loop as the two
previous examples.
This DO WHILE produces
ILOOP=l; the same results.

SUM=O;
DO WHILE(ILOOP LT 5);
SUM=SUM+ILOOP;
ILOOP+l;
END;

22 SAS ESSENTIALS -- Elliott & Woodward


DO Loop with Arrays
 One of the most powerful uses of the DO loop is realized
when it is used in conjunction with an array.
 For example, suppose you take five systolic blood
pressure readings on each subject.
 Suppose that if any of these readings is above 140, you
want to classify the subject with the value HIGHBP=1, or
else the subject will have the value HIGHBP=0. The
following code could perform that task:

23 SAS ESSENTIALS -- Elliott & Woodward


The ARRAY statement sets
DO Loop with Arrays Example up an array named SBP
that contains the values of
the variables READING1
DATA PRESSURE;SET MEDDATA; through READING5.

ARRAY SBP(5) READINGl-READING5;


Note the initial value of HIGHBP is set at 0.
HIGHBP=0;
In the DO statement, the variable I
iteratively takes on the values from 1 to 5.
DO I=l TO 5;

IF SBP(I) GT 140 THEN HIGHBP=l;


Within the DO loop, if any of the five readings is GT 140, the
variable HIGHBP is set to 1. Otherwise, the value of HIGHBP
END; remains at the initial value of 0.
24 SAS ESSENTIALS -- Elliott & Woodward
Similar Example using a Text Array
DATA NEW;SET OLD; The Array contains the values of Q1
through Q50.

ARRAY ANSWER(l:50) $ Ql-Q50;


The IF statement is performed 50 times, each
DO I=l TO 50; time with a different value of ANSWER (I)

IF ANSWER(I)="NA" then ANSWER(I)="";


Each of the 50 questions is examined. If an
END; answer is "NA" it is recoded with value " " (a
blank).

 Do Hands on Example p 171 (DDOLOOP.SAS)

25 SAS ESSENTIALS -- Elliott & Woodward


IF and DO
 The primary DO statement discussed in this chapter is the
iterative DO loop. An iterative DO loop
 is a way of incrementing through series of statements
based on some index. A simplified
 syntax of an iterative DO loop is as follows:

26 SAS ESSENTIALS -- Elliott & Woodward


Using Values in an Array
 From the Hands On Example (DARRAY1.SAS)
Different ways that
DATA A; the values in the T()
array are used in the
INPUT ONE TWO THREE FOUR; SUM function

ARRAY T(4) ONE TWO THREE FOUR;


TSUM1=SUM(OF ONE TWO THREE FOUR);
TSUM2=SUM(OF T(1) T(2) T(3) T(4));
TSUM3=SUM(OF T(*));

All with same result

27 SAS ESSENTIALS -- Elliott & Woodward


Do Hands On Exercise p 172
 Example of HBOUND and LBOUND (DARRAY3.SAS)
DATA CRIME;SET "C:\SASDATA\DC_CRIME78_07";
FORMAT TOTAL 6.;
ARRAY INCIDENTS(*) VIOLENT--CARTHEFT;
DO I= LBOUND(INCIDENTS) to
HBOUND(INCIDENTS);
TOTAL=SUM(of TOTAL,INCIDENTS(i));
END;
Note the use of LBOUND
DROP I; and HBOUND to define
RUN; limits of the loop.

28 SAS ESSENTIALS -- Elliott & Woodward


7.3 USING THE RETAIN STATEMENT
 The RETAIN statement, used in a DATA step, allows you to
retain values of a variable across iterations as data
records are read into the data set. The basic syntax for
RETAIN is:
RETAIN <var <initial-value(s)>
var2 <initial-values(s)>
and so on;
 For example, to instruct SAS to remember the value of
the variable SCORE, you would use the statement

RETAIN SCORE;
29 SAS ESSENTIALS -- Elliott & Woodward
Setting Values in RETAIN
 A statement that retains the values of several variables
could be
RETAIN TIME1-TIME10 SCORE VISITDATE;
 By default, the initial value of retained variables is
missing. A statement to retain the value of SCORE, with
an initial value of 1, is
 RETAIN SCORE 1;
 In the following, CATEGORY has an initial value of NONE
and variables TIME1-TIME10 have initial values of 0.
 RETAIN CATEGORY "NONE" TIME1-TIME10 0;

30 SAS ESSENTIALS -- Elliott & Woodward


More Setting Values in RETAIN
 To set the values of a list, use this format:
RETAIN TIME1-TIME4 (1 2 3 4);
 or
RETAIN TIME1-TIME4 (1, 2, 3, 4);
 or
RETAIN TIME1-TIME4 (1:4);

To retain the values of all variables, use


RETAIN _ALL_;

 Do Hands On Example p 175 (DRETAIN1.SAS)

31 SAS ESSENTIALS -- Elliott & Woodward


DRETAIN1.SAS Example
DATA DAYS;SET MYDATA;
IF _N_=1 THEN FIRST=VISIT_DATE;
Note RETAIN statement (it appears in code
RETAIN FIRST; after the initial instance of the variable FIRST.)
DAYS=VISIT_DATE-FIRST;
RUN; Note in the output how the initial value of FIRST is
“remembered” for all observations.

32 SAS ESSENTIALS -- Elliott & Woodward


Second RETAIN Hands On Example (DRETAIN2.SAS)
DATA LEMON;
INPUT DAY SALES;
IF _N_=1 THEN TOTAL=0;
RETAIN TOTAL MAX;
TOTAL=SALES+TOTAL;
IF SALES>MAX then MAX=SALES;
DATALINES;
In this code, RETAIN is
Etc… used to accumulate
TOTAL, and to find the
MAX value

33 SAS ESSENTIALS -- Elliott & Woodward


7.4 USING SAS MACROS
 Macros, a powerful programming tool within
SAS, allow you to
 set aside repetitive sections of code and to
use them again and again when needed,
 create dynamic variables (macro variables)
within the code that can take on specified
values.

34 SAS ESSENTIALS -- Elliott & Woodward


Two Macro Types Discusses
 Macro Variable – a symbolic name that stores
information that can be used to dynamically modify SAS
code through symbolic substitution.
 Macro Process – a set of SAS code that can be “called” to
do repetitive tasks
 These two Macro types are briefly discussed in this
section…

35 SAS ESSENTIALS -- Elliott & Woodward


Creating and Using SAS Macro Variables
 A SAS macro variable is a powerful programming concept
that allows you to write code that can be easily changed
to reflect new values for selected variables or code.
 A SAS macro variable is the symbolic name that stores
information that can be used to dynamically modify SAS
code through symbolic substitution.
 You can think of this substitution in a way that is similar
to a "replace" in a word processor.
 The general format for creating a macro variable is as
follows:
%LET macrovariablename = text;

36 SAS ESSENTIALS -- Elliott & Woodward


Macro Variables
 For example, suppose you have a program that calculates
values based on a given year and a comparison based on
the value DEPT. You can create macro variables called
YEAR and DEPT using the statements
%LET YEAR=2015;
%LET DEPT=ENGINEERING;
 Refer to the macro variable by placing an & (ampersand)
as a prefix to the variable name. Thus, once you define
YEAR as a macro variable, you use it in a statement in the
When the SAS code is run, the
following way: &YEAR is replaced by 2015 (or
IND_DAY=MDY(7,4,&YEAR); whatever value you previously set
in the %LET YEAR= statement.

37 SAS ESSENTIALS -- Elliott & Woodward


More Macro Variable Substitution
In the case of the macro variable DEPT, the statement
GROUP = "&DEPT";
 is "seen" by SAS as the code
GROUP= "ENGINEERING";
 The &DEPT macro variable is replaced with its value
ENGINEERING. The quotations were required in the
statement to make the completed statement resolve into
acceptable SAS code. The following code uses these two
macro variables within a TITLE statement:

TITLE "Analysis for &DEPT for the


year &YEAR";
38 SAS ESSENTIALS -- Elliott & Woodward
Additional Information about Macro Variables
 If there is no text provided in a %LET statement, the
contents of the macro variable is a null value (0
characters). For example,
%LET ISNULL=;
 It is important to understand that any leading or trailing
blanks in the macro text value are ignored. For example,
%LET CITY=DALLAS;
 produces the same result as code that has several blanks
around the name:
%LET CITY = DALLAS ;
 The blanks in front of or after the text are ignored.
 Do Hands On Example p 179. (DMACRO1.SAS)

39 SAS ESSENTIALS -- Elliott & Woodward


Combining Macro Variables
 Sometimes you want to combine macro variables:
%LET PATH=C:\SASDATA\;
%LET DSN=SOMEDATA;
%LET CLASS=GP;
%LET SELECTVAR=AGE TIME1-TIME4;
 …and then you use these macro variables:
PROC MEANS DATA="&PATH&DSN" MAXDEC=2;
CLASS &CLASS ;
VAR &SELECTVAR; Note how putting the two macro
variables together creates the
RUN; data set name
C:\SASDATA\SOMEDATA

40 SAS ESSENTIALS -- Elliott & Woodward


More putting together macro variables
 If there is ambiguity in how two items are concatenated,
you can use a dot(.) as a separator. For example, suppose
you have
%LET STATUS=PRE;
 If you want to combine &STATUS with the word
PRODUCTION, and if you used "&STATUSPRODUCTION",
SAS could not resolve the macro variable correctly. Using
the code "&STATUS. PRODUCTION" works.
 Do Hands On Example p 181. (DMACRO2.SAS)

41 SAS ESSENTIALS -- Elliott & Woodward


Creating Callable Macro Routines
 A SAS macro is a series of SAS statements that performs a
general task, such as creating a report. Instead of "hard
coding" this routine with fixed values, you write your
code so that certain portions of the code can be altered
using macro variables.
 Once a macro routine is defined properly, you can use it
again and again in your program.
 This is referred to as "calling" the macro.

42 SAS ESSENTIALS -- Elliott & Woodward


A Callable Macro Routine (Syntax)
 The simplified syntax of a SAS macro is
Begin a SAS macro with a
%MACRO macroname statement

%MACRO macroname <(parameter- list )>


</options>;

Put SAS code here – it often


*Put SAS code here; contains macro variable names

%MEND macroname;
End the SAS macro with a
%MEND statement.
43 SAS ESSENTIALS -- Elliott & Woodward
An explanation of how macro code works:
 The information between the %MACRO statement and the
%MEND statement is a SAS macro routine consisting of SAS
code.
 The macroname is the name you give to the macro that obeys
standard SAS naming conventions.
 The parameter list indicates a way to "send" information into
the macro routine (illustrated in an upcoming example).
 Within the SAS macro (between %MACRO to %MEND), the
variables referenced in the parameter list become macro
variables within the macro and may be referred to withthe
standard macro variable syntax and variablename.

44 SAS ESSENTIALS -- Elliott & Woodward


Example SAS Macro The variables in the parameter
list “send” information in the
form of macro variables
%MACRO REPORT(SUBJ, DSN); Notice
Notice how
how the
the code
code within
within
DATA REPORT;SET "&DSN"; the
the macro
macro references
references the
the
“sent”
“sent” macro
macro variables
variables such
such
IF SUBJ=&SUBJ; as
as &DSN
&DSN
TITLE "REPORT ON SUBJECT# &SUBJ";
PROC PRINT NOOBS DATA=REPORT;
VAR GENDER TIME EXPOSED DIAGNOSED;
RUN;
%MEND REPORT; Once the macro is defined in the %MACRO to
%MEND statements, you call it with this statement:

%REPORT(SUBJ=001, DSN=C:\SASDATA\SUBJECTS);

Note the name of the called macro. The macro is named


45
REPORT, SAS
andESSENTIALS
called as--%REPORT
Elliott & Woodward
What happens when the macro is called:
 The REPORT macro is compiled by SAS, a call to it using the
example information does the following:
1. The values assigned to SUBJ and DSN are sent to the macro routine.
2. Within the routine wherever &SUBJ is found, it is substituted for the
value of SUBJ. Wherever &DSN is found, it is substituted for the value
of DSN.
3. For this example, the SAS code after the substitutions becomes
DATA REPORT;SET "C:\SASDATA\SUBJECTS ";
IF SUBJ=001;
TITLE "REPORT ON SUBJECT# 001";
PROC PRINT NOOBS DATA=REPORT;
VAR GENDER TIME EXPOSED DIAGNOSED;
RUN;
4. The revised code is run by SAS.

46 SAS ESSENTIALS -- Elliott & Woodward


How a SAS Macro Works…

Do the Hands
On Example on
P 184

47 SAS ESSENTIALS -- Elliott & Woodward


Using a Macro with No Parameters
 A simple macro is one that contains no parameters. In this
case, the macro routine is defined as
Notice no parameters
named
%MACRO macroname;
SAS code;
%MEND macroname;

48 SAS ESSENTIALS -- Elliott & Woodward


Including SAS Macro Code
 You may create a number of SAS macro routines that you want
to use again and again. Instead of including the macro code in
the program where you are going to call it, you may want to
include the macro code dynamically in your program. For
example, the file that contains the DISCLAIM macro is
DMACRODISCLAIM . SAS. By placing a %INCLUDE command,
given here, at the top of your code, you can make this macro
available for use in a program.

%INCLUDE "C:\SASDATA\DMACRODISCLAIM.SAS";

 Do Hands on Example p 186 (DMACDISCLAIM.SAS)

49 SAS ESSENTIALS -- Elliott & Woodward


Using The SAS Macro %D0 Loop
 The SAS %DO loop statement is similar to the previously
discussed DO loop, but in this macro version (note the
percentage sign before the DO), you are able to define
increment macro variables that take on dynamic values.
 The %DO loop statement must appear inside a SAS macro
routine, but unlike the standard DO loop, it does not have to
appear within a DATA step. A simplified syntax is as follows:

%D0 macrovariable=start %TO stop <%BY inc>;


SAS statements;
%END;

50 SAS ESSENTIALS -- Elliott & Woodward


Example of a %DO Loop
 For example, the following %DO loop iterates from the
variable I (which we refer to as &I, a macro variable) to 5 and
assigns the name of a DATA file based on the value of &I.
%DO I = L %TO 5;
DATA GP&I;
SET MYSASLIB.SOMEDATA;
WHERE STATUS=&I ;
RUN;
%END;
 The code GP&I resolves to the data set names GP1, GP2,
GP3, and so on within the loop as I increments from 1 to 5.
 Do Hands On Example p 188.

51 SAS ESSENTIALS -- Elliott & Woodward


Using CALL SYMPUT to Create a Macro Variable
 The SAS CALL SYMPUT routine assigns a value to a
macro variable within a DATA step.
 The call does not have to be within a macro routine,
although it can be. The syntax of the call is as follows:

CALL SYMPUT ('macrovariablename',value);

 The macrovariablename is the name you want to assign a


value.
 Do Hands On Example p 189. (DMACRO5.SAS)

52 SAS ESSENTIALS -- Elliott & Woodward


7.5 SUMMARY
 This chapter provides information on two topics that
allow you to write SAS code that can simplify complex
procedures: mainly arrays and macros. Arrays can simplify
creating and using large data constructs that must be
manipulated by code.
 Macro variables allow you to create generalized
programs, and macro routines allow you to create code
that can be used over and over again.
 Go to Chapter 8: CONTROLLING OUTPUT USING ODS

53 SAS ESSENTIALS -- Elliott & Woodward


These slides are based on the book:

Introduction to SAS Essentials


Mastering SAS for Data Analytics, 2nd Edition

By Alan C, Elliott and Wayne A. Woodward

Paperback: 512 pages


Publisher: Wiley; 2 edition (August 3, 2015)
Language: English
ISBN-10: 111904216X
ISBN-13: 978-1119042167

These slides are provided for you to use to teach SAS using this book. Feel free to
modify them for your own needs. Please send comments about errors in the slides
(or suggestions for improvements) to acelliott@smu.edu. Thanks.

54 SAS ESSENTIALS -- Elliott & Woodward

You might also like