Download presentation
Presentation is loading. Please wait.
Published byRosa Roberts Modified over 5 years ago
1
You deserve ARRAYs! How to be more efficient using SAS®
By: Kate Burnett-Isaacs Presented at SAS Global Forum 2015
2
Introduction An array is a group of related variables defined in a DATA step Arrays are extremely useful when a repetitive process, such as a calculation, transformation or function, needs to be applied to a large set of variables An array enables the programmer to set a list of variables once and then refer this list, or elements within the list, any number of times within a single DATA step #SASGF13 Copyright © 2013, SAS Institute Inc. All rights reserved.
3
Establishing The Array
We define an array in an ARRAY statement within the DATA step by the keyword ‘array’ : Array name: array_name Length of the array: {N} or {*} Variables or array elements that it will contain: variable1 … variableN All array elements must be character or numeric array array_name {N} variable1 … variableN; #SASGF13 Copyright © 2013, SAS Institute Inc. All rights reserved.
4
Using The Array The number in the braces indicates the corresponding element in the array To refer variable2: To apply a repetitive process to a large set of variables, use the DO loop: Use all of the array elements together in the calculation of the sum or mean with OF operator: array_name {2}; do i=1 to dim(array_name); array_name {i}=i; end; Total=sum(of array_name {*}); #SASGF13 Copyright © 2013, SAS Institute Inc. All rights reserved.
5
Example You are an employer, whose employees took a week long SAS course If they were absent, no grade is present To reward participation, each employee gets 2% of their weekly earnings for every day they attend the course You want to show your employees how much they made each day of the course and what their total weekly salary will be at the end of the week long course #SASGF13 Copyright © 2013, SAS Institute Inc. All rights reserved.
6
Example Dataset #SASGF13 Copyright © 2013, SAS Institute Inc. All rights reserved.
7
Example with IF-ELSE Statement
data No_Arrays; set SAS_scores; if NOT MISSING(Monday) then BnsMon=(Wkearn*0.02); else BnsMon=0; if NOT MISSING(Tuesday) then BnsTues=(Wkearn*0.02); else BnsTues=0; if NOT MISSING(Wednesday) then BnsWed=(Wkearn*0.02); else BnsWed=0; if NOT MISSING(Thursday) then BnsThurs=(Wkearn*0.02); else BnsThurs=0; if NOT MISSING(Friday) then BnsFri=(Wkearn*0.02); else BnsFri=0; fnlearn=wkearn+sum(BnsMon, BnsTues, BnsWed, BnsThurs, BnsFri); keep Name BnsMon BnsTues abWed BnsThurs BnsFri fnlearn; run; we would use an IF-ELSE statement for each day of the week in order to create a daily bonus variable (bnsMon-bnsFri) that reflects the amount earned by each employee for each day of the week they attended the SAS course. In order to determine the final weekly earnings (fnlearn), any daily bonuses attained will be added to the weekly earnings of each employee This method is fairly tedious to code and error-prone. Imagine the number of IF-ELSE statements needed to conduct these calculations over several months or even a year, instead of a week! #SASGF13 Copyright © 2013, SAS Institute Inc. All rights reserved.
8
Example with Arrays data Using_Arrays; set SAS_scores;
array array_day {*} Monday Tuesday Wednesday Thursday Friday; array bonus {*} BnsMon BnsTues BnsWed BnsThurs BnsFri; do i=1 to dim(array_day); if NOT MISSING(array_day {i}) then bonus{i}=Wkearn*(0.02); else bonus {i}=0; end; fnlearn=(wkearn+sum(of bonus {*})); keep Name BnsMon BnsTues abWed BnsThurs BnsFri fnlearn; run; #SASGF13 Copyright © 2013, SAS Institute Inc. All rights reserved.
9
Results Copyright © 2013, SAS Institute Inc. All rights reserved.
#SASGF13 Copyright © 2013, SAS Institute Inc. All rights reserved.
10
Conclusion Arrays can easily scale with the size of the program and the data, the time and effort saved will also increase accordingly An array is a very useful tool when repetitively processing large sets of variables. Arrays allow repetitive code to be more efficiently executed and less prone to coding mistakes. Use arrays and in no time you will receive your raise! #SASGF13 Copyright © 2013, SAS Institute Inc. All rights reserved.
11
The SAS Global Forum Experience
OASUS Spring or Fall YYYY Thursday, February-28-19 The SAS Global Forum Experience First & last name Company name
12
OASUS Spring or Fall YYYY
Thursday, February-28-19 Process Idea Session Choice Application Process Presenting First & last name Company name
13
How to Choose a Paper Topic
What you found challenging in your own SAS experience What you would like to learn more about Some code, techniques or process that you have created which is rather unique
14
Session Choice Quick Tip Breakout Session Poster Table Talk
15
Application Process Apply on SAS Global Forum website Abstract
Draft paper Catchy title
16
Presenting Minimal text or code on the slides
At most, one slide per minute Leave time for questions Know your audience
17
OASUS Spring or Fall YYYY
Thursday, February-28-19 At SAS Global Forum Experience as much as you can Choose the presentations you attend wisely Network First & last name Company name
18
OASUS Spring or Fall YYYY
Thursday, February-28-19 Questions? Thursday, February-28-19 First & last name Company name First & last name Company name
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.