Download presentation
Presentation is loading. Please wait.
Published byRaymond Elliott Modified over 8 years ago
1
Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS ESSENTIALS -- Elliott & Woodward1
2
Chapter 7: Advanced Programming Topics Part 2 SAS ESSENTIALS -- Elliott & Woodward2
3
LEARNING OBJECTIVES SAS ESSENTIALS -- Elliott & Woodward3 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
4
7.1 USING SAS ARRAYS SAS ESSENTIALS -- Elliott & Woodward4 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;
5
One-Dimensional Arrays SAS ESSENTIALS -- Elliott & Woodward5 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 TIME1 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;
6
Specify Beginning and Ending Index Value SAS ESSENTIALS -- Elliott & Woodward6 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.
7
Unknown number of elements in an array SAS ESSENTIALS -- Elliott & Woodward7 Consider this array: ARRAY ANSWER(*) Q1 - QSO; 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.
8
Specify an array of text values SAS ESSENTIALS -- Elliott & Woodward8 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.
9
Specify Actual Values for a Created Array SAS ESSENTIALS -- Elliott & Woodward9 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
10
Specify actual character values for an array SAS ESSENTIALS -- Elliott & Woodward10 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. Character Specify actual values
11
Specify no values in an array SAS ESSENTIALS -- Elliott & Woodward11 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. All values missing
12
Specify initial values for items in an array SAS ESSENTIALS -- Elliott & Woodward12 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
13
LBOUND, HBOUND, and DIM() SAS ESSENTIALS -- Elliott & Woodward13 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.
14
Referring to Values in an Array SAS ESSENTIALS -- Elliott & Woodward14 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. Do Hands on Example p 165 – using values in an array
15
Searching an Array SAS ESSENTIALS -- Elliott & Woodward15 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 MY ARRAY. Do the Hands on Example p 166.
16
Searching for a Value in an Array SAS ESSENTIALS -- Elliott & Woodward16 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 ; This is what it should find
17
7.2 USING DO LOOPS SAS ESSENTIALS -- Elliott & Woodward17 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
Iterative DO LOOP SAS ESSENTIALS -- Elliott & Woodward18 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 ; SAS statements... END; For example, SUM=O; DO ILOOP=1 to 4; SUM=SUM + ILOOP; END; The INDEXVALUE is named ILOOP. ILOOP “iterates” from a starting value of 1 to and ending value of 4
19
Follow the Iterations of the Loop SAS ESSENTIALS -- Elliott & Woodward19 SUM=0; DO ILOOP=l to 4; SUM=SUM+ILOOP; END; Follow the code: Since initially, SUM=0 and the first value of ILOOP=1 then the first time through SUM=0 + 1; Subsequent loops are: SUM=1+2; SUM=3+3; SUM=6+4; So,the final value of SUM at the end of the loop is 10.
20
A DO UNTIL Loop SAS ESSENTIALS -- Elliott & Woodward20 In a DO UNTIL loop, the increment is replaced with an UNTIL clause with (ILOOP GT 4) in parentheses. For example, ILOOP=l; SUM=O; DO UNTIL (ILOOP GT 4); SUM=SUM+ILOOP; ILOOP+l; END; Do Hands on Example p 169 (DOWHILE.SAS) This code produces the same loop as the previous example, using DO UNTIL instead of the Iterative Do Loop
21
A DO WHILE Loop SAS ESSENTIALS -- Elliott & Woodward21 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. ILOOP=l; SUM=O; DO WHILE(ILOOP LT 5); SUM=SUM+ILOOP; ILOOP+l; END; This DO WHILE produces the same results.
22
DO Loop with Arrays SAS ESSENTIALS -- Elliott & Woodward22 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
DO Loop with Arrays Example SAS ESSENTIALS -- Elliott & Woodward23 DATA PRESSURE;SET MEDDATA; ARRAY SBP(S) READINGl-READING5; HIGHBP=0; DO I=l TO 5; IF SBP(I) GT 140 THEN HIGHBP=l; END; The ARRAY statement sets up an array named SBP that contains the values of the variables READING1 through READING5. In the DO statement, the variable I iteratively takes on the values from 1 to 5. 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 remains at the initial value of 0. Note the initial value of HIGHBP is set at 0.
24
Similar Example using a Text Array SAS ESSENTIALS -- Elliott & Woodward24 DATA NEW;SET OLD; ARRAY ANSWER(l:5O) $ Ql-Q5O; DO I=l TO 50; IF ANSWER(I)="NA" then ANSWER(I)=""; END; Do Hands on Example p 171 (DDOLOOP.SAS) The Array contains the values of Q1 through Q50. The IF statement is performed 50 times, each time with a different value of ANSWER (I) Each of the 50 questions is examined. If an answer is "NA" it is recoded with value " " (a blank).
25
IF and DO SAS ESSENTIALS -- Elliott & Woodward25 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
Using Values in an Array SAS ESSENTIALS -- Elliott & Woodward26 From the Hands On Example (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(*)); Different ways that the values in the T() array are used in the SUM function All with same result
27
Do Hands On Exercise p 172 SAS ESSENTIALS -- Elliott & Woodward27 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 and HBOUND to define limits of the loop.
28
7.3 USING THE RETAIN STATEMENT SAS ESSENTIALS -- Elliott & Woodward28 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 var2 and so on; For example, to instruct SAS to remember the value of the variable SCORE, you would use the statement RETAIN SCORE;
29
Setting Values in RETAIN SAS ESSENTIALS -- Elliott & Woodward29 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
More Setting Values in RETAIN SAS ESSENTIALS -- Elliott & Woodward30 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
DRETAIN1.SAS Example SAS ESSENTIALS -- Elliott & Woodward31 DATA DAYS;SET MYDATA; IF _N_=1 THEN FIRST=VISIT_DATE; RETAIN FIRST; DAYS=VISIT_DATE-FIRST; RUN; Note RETAIN statement (it appears in code after the initial instance of the variable FIRST.) Note in the output how the initial value of FIRST is “remembered” for all observations.
32
Second RETAIN Hands On Example (DRETAIN2.SAS) SAS ESSENTIALS -- Elliott & Woodward32 DATA LEMON; INPUT DAY SALES; IF _N_=1 then DO; TOTAL=0; END; RETAIN TOTAL MAX; TOTAL=SALES+TOTAL; IF SALES>MAX then MAX=SALES; DATALINES; In this code, RETAIN is used to accumulate TOTAL, and to find the MAX value
33
7.4 USING SAS MACROS SAS ESSENTIALS -- Elliott & Woodward33 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
Two Macro Types Discusses SAS ESSENTIALS -- Elliott & Woodward34 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
Creating and Using SAS Macro Variables SAS ESSENTIALS -- Elliott & Woodward35 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
Macro Variables SAS ESSENTIALS -- Elliott & Woodward36 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 following way: IND_DAY=MDY(7,4,&YEAR); When the SAS code is run, the &YEAR is replaced by 2015 (or whatever value you previously set in the %LET YEAR= statement.
37
More Macro Variable Substitution SAS ESSENTIALS -- Elliott & Woodward37 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
Additional Information about Macro Variables SAS ESSENTIALS -- Elliott & Woodward38 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.
39
Combining Macro Variables SAS ESSENTIALS -- Elliott & Woodward39 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; RUN; Note how putting the two macro variables together creates the data set name C:\SASDATA\SOMEDATA
40
More putting together macro variables SAS ESSENTIALS -- Elliott & Woodward40 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 &PRE with the word PRODUCTION, and if you used "&PREPRODUCTION", SAS could not resolve the macro variable correctly. Using the code "&PRE. PRODUCTION" works. Do Hands On Example p 181.
41
Creating Callable Macro Routines SAS ESSENTIALS -- Elliott & Woodward41 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
A Callable Macro Routine (Syntax) SAS ESSENTIALS -- Elliott & Woodward42 The simplified syntax of a SAS macro is %MACRO macroname ; *Put SAS code here; %MEND macroname; Begin a SAS macro with a %MACRO macroname statement Put SAS code here – it often contains macro variable names End the SAS macro with a %MEND statement.
43
An explanation of how macro code works: SAS ESSENTIALS -- Elliott & Woodward43 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
Example SAS Macro SAS ESSENTIALS -- Elliott & Woodward44 %MACRO REPORT(SUBJ, DSN); DATA REPORT;SET "&DSN"; IF SUBJ=&SUBJ; TITLE "REPORT ON SUBJECT# &SUBJ"; PROC PRINT NOOBS DATA=REPORT; VAR GENDER TIME EXPOSED DIAGNOSED; RUN; %MEND REPORT; %REPORT(SUBJ=001, DSN=C: \SASDATA\SUBJECTS); Notice how the code within the macro references the “sent” macro variables such as &DSN The variables in the parameter list “send” information in the form of macro variables Notice how the code within the macro references the “sent” macro variables such as &DSN Once the macro is defined in the %MACRO to %MEND statements, you call it with this statement: Note the name of the called macro. The macro is named REPORT, and called as %REPORT
45
What happens when the macro is called: SAS ESSENTIALS -- Elliott & Woodward45 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
How a SAS Macro Works… SAS ESSENTIALS -- Elliott & Woodward46 Do the Hands On Example on P 184
47
Using a Macro with No Parameters SAS ESSENTIALS -- Elliott & Woodward47 A simple macro is one that contains no parameters. In this case, the macro routine is defined as %MACRO macroname; SAS code; %MEND macroname ; Notice no parameters named
48
Including SAS Macro Code SAS ESSENTIALS -- Elliott & Woodward48 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
Using The SAS Macro %D0 Loop SAS ESSENTIALS -- Elliott & Woodward49 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 ; SAS statements; %END;
50
Example of a %DO Loop SAS ESSENTIALS -- Elliott & Woodward50 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
Using CALL SYMPUT to Create a Macro Variable SAS ESSENTIALS -- Elliott & Woodward51 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.
52
7.5 SUMMARY SAS ESSENTIALS -- Elliott & Woodward52 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.