Array PDF
Array PDF
1
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
2 One-Dimensional Arrays
3 Multidimensional Arrays
2
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
2 One-Dimensional Arrays
3 Multidimensional Arrays
3
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Objectives
◼ Define table lookup.
◼ List table lookup techniques.
4
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Introduction
There is often a need to combine or look up data from
multiple sources to create meaningful reports.
With the volume of data that exists today, you might often need to combine or
look up data from multiple sources to create meaningful reports.
5
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Introduction
When data sources do not share a common structure,
a lookup table can match them.
6
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Continent Continent
ID Name
91 North America
93 Europe
94 Africa
95 Asia
96 Australia/Pacific
7
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
IF-THEN/ELSE Statements
Lookup tables can be SAS programming statements.
data countryinfo;
set [Link];
if ContinentID=91
then Continent='North America';
else if ContinentID=93
then Continent='Europe';
else if ContinentID=94
then Continent='Africa';
else if ContinentID=95
then Continent='Asia';
else if ContinentID=96
then Continent='Australia/Pacific';
run;
8
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
9
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
10
10
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
User-Defined Formats
Lookup tables can be user-defined formats that are
accessed with a FORMAT statement or PUT function.
proc format;
value ContName
91='North America' 93='Europe'
94='Africa' 95='Asia'
96='Australia/Pacific';
run;
proc print data=[Link];
format ContinentID ContName.;
run;
data countryinfo;
set [Link];
Continent=put(ContinentID,ContName.);
run;
11
11
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Arrays
Lookup tables can be arrays.
data countryinfo;
array ContName{91:96} $ 30 _temporary_
('North America',
' ',
'Europe',
'Africa',
'Asia',
'Australia/Pacific');
set [Link];
Continent=ContName{ContinentID};
run;
p1
12
12
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
13
13
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
2 One-Dimensional Arrays
3 Multidimensional Arrays
14
14
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
Objectives
◼ Explain the concepts of SAS arrays.
◼ Use SAS arrays to perform repetitive calculations.
◼ Use arrays as arguments to SAS functions.
◼ Explain array functions.
◼ Use arrays to create new variables.
◼ Use arrays to perform a table lookup
15
15
15
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
Array Processing
You can use arrays to simplify programs that do the
following:
◼ perform repetitive calculations
◼ create many variables with the same attributes
◼ read data
◼ compare variables
16
16
16
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Business Scenario
The orion.employee_donations data set contains
quarterly contribution data for each employee. Orion
management is considering a 25% matching program.
Calculate each employee’s quarterly contribution, including
the proposed company supplement.
17
17
17
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
120265 . . . 31.25
120267 18.75 18.75 18.75 18.75
120269 25.00 25.00 25.00 25.00
120270 25.00 12.50 6.25 .
p2
18
18
Looking at this you may think – let’s use a DO loop so that we can replace the
4 calculations with a single calculation, but…
Note: The KEEP statement results in droppping the two unwanted variables,
Paid_By and Recipients.
18
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
19
19
19
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
20
20
20
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
21
21
21
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
22
22
SAS arrays are different from arrays in many other programming languages. In
SAS, an array is not a data structure. It is simply a convenient way of
temporarily identifying a group of variables.
We’ll define an array named CONTRIB and use it to refer to the 4 variables,
Qtr1-Qtr4 using a common name. This will allow us to use a DO loop to
access the array elements.
22
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
Array Elements
Each value in an array is called an element.
23
23
23
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
array references
24
24
And each element is accessed using the array name and a subscript. Contrib-
sub-1 is an alternate way of referring to Qtr1. Any time you use Contrib{1}
the value of Qtr1 will be used. The same is true for Contrib{2} thru
Contrib{4}.
24
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
Defining an Array
The ARRAY statement is a compile-time statement
that defines the elements in an array.
ARRAY array-name {subscript} <$> <length>
<array-elements>;
25
25
25
The array Contrib has 4 elements. The four variables, Qtr1, Qtr2, Qtr3, and Qtr4, can
now be referenced via the array name Contrib, with an appropriate subscript.
25
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
Defining an Array
An alternate syntax uses an asterisk instead of a subscript.
SAS determines the subscript by counting the variables in
the element list. The element list must be included.
26
26
An asterisk can be used instead of a subscript in the array definition to let SAS
determine the number of elements in the array. This is convenient when using
a variable list.
You can use special SAS name lists to reference variables that were previously
defined in the same DATA step. The _CHARACTER_ variable lists character
values only. The _NUMERIC_ variable lists numeric values only.
Avoid using the _ALL_ special SAS name list to reference variables, because
the elements in an array must be either all character or all numeric values.
26
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
Defining an Array
Variables that are elements of an array do not need
the following:
◼ to have similar, related, or numbered names
◼ to be stored sequentially
◼ to be adjacent
AMT
PDV
ID Q1 Total ThrdQ Q2 Qtr4
27
27
The elements need not be adjacent nor have common variable names.
27
C o p yr i g h t © 2 0 1 4 , S AS In s t i t u t e In c . Al l r i g h t s r e s e r ve d .
28
If when you define the array, you only specify one number for your one dimension, you
are specifying an upper bound. This is an implicit method which assumes a lower bound
of 1 to an upper bound equal to the number of elements. In this example, 1 to 3. An
alternative syntax for writing this one dimension, is to explicitly specify a lower bound
followed by a colon and then an upper bound. A value of 1 colon 3 is a lower bound
starting at 1 and going to the upper bound of 3. When specifying bounds, you must
specify values from a low value to a high value.
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
p3
29
The subscript and the number of elements in the list do not agree.
29
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
30
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
p4
31
31
The DIM( ) function can be used to determine how many elements are in an
arry. It is covered in the next section.
The index variable, i, is not written to the output data set because it is not
listed in the KEEP statement.
31
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
when i=1
Contrib{1}=Contrib{1}*1.25;
Qtr1=Qtr1*1.25;
32
32 ...
32
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
when i=2
Contrib{2}=Contrib{2}*1.25;
Qtr2=Qtr2*1.25;
33
33 ...
33
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
when i=3
Contrib{3}=Contrib{3}*1.25;
Qtr3=Qtr3*1.25;
34
34 ...
34
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
when i=4
Contrib{4}=Contrib{4}*1.25;
Qtr4=Qtr4*1.25;
35
35
35
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
120265 . . . 31.25
120267 18.75 18.75 18.75 18.75
120269 25.00 25.00 25.00 25.00
120270 25.00 12.50 6.25 .
120271 25.00 25.00 25.00 25.00
120272 12.50 12.50 12.50 12.50
120275 18.75 18.75 18.75 18.75
120660 31.25 31.25 31.25 31.25
120662 12.50 . 6.25 6.25
36
36
36
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
37
37
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
1 120265 25 25
2 120267 60 60
3 120269 80 80 p5
38
38
You cannot refer to an entire array just by its name, but you can use
arrayName{*}. In this example the entire array is passed to the SUM function
as a variable list.
38
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
DIM Function
The DIM function returns the number of elements
in an array. This value is often used as the stop value
in a DO loop.
data charity;
set orion.employee_donations;
keep Employee_ID Qtr1-Qtr4;
array Contrib{*} qtr:;
do i=1 to dim(Contrib);
Contrib{i}=Contrib{i}*1.25;
end;
run;
DIM(array_name)
P6
39
39
The DIM function returns the number of elements in an array. Here it is used
to determine the stop value for the loop.
The HBOUND function returns the upper bound of an array. The LBOUND
function returns the lower bound of an array. The following DO loop processes
every element in an array:
do i=lbound(items) to hbound(items);
*process items{i};
end;
In most arrays, subscripts range from 1 to n, where nis the number of elements
in the array. The value 1 is a convenient lower bound. Thus, you do not need to
specify the lower bound. However, specifying both bounds is useful when the
array dimensions have a beginning point other than 1. For example, the
following ARRAY statement defines an array of 10 elements, with subscripts
that range from 5 to 14:
array items{5:14} n5-n14;
39
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
40
40
40
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
PDV
Month1 Month2 Month3 Month4 Month5 Month6
$ 10 $ 10 $ 10 $ 10 $ 10 $ 10
41
41
Creating multiple character variables. They are all the same length.
41
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
Business Scenario
Using orion.employee_donations as input, calculate
the percentage that each quarterly contribution
represents of the employee’s total annual contribution.
Create four new variables to hold the percentages.
Employee_ID Qtr1 Qtr2 Qtr3 Qtr4
120265 . . . 25
120267 15 15 15 15
120265 . . . 100%
%
120267 25% 25% 25% 25%
42
42
42
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
p7
43
43
43
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
120265 . . . 100%
120267 25% 25% 25% 25%
120269 25% 25% 25% 25%
120270 57% 29% 14% .
120271 25% 25% 25% 25%
120272 25% 25% 25% 25%
120275 25% 25% 25% 25%
120660 25% 25% 25% 25%
120662 50% . 25% 25%
120663 . . 100% .
120668 25% 25% 25% 25%
44
44
Display the ID and the 4 new variables. Use a Percent6. format for the new
variables. The PERCENTw.d format multiplies values by 100, formats them in
the same way as the BESTw.d format and adds a percent sign (%) to the end of
the formatted value. Negative values are enclosed in parentheses. The
PERCENTw.d format provides room for a percent sign and parentheses, even
if the value is not negative.
44
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
Business Scenario
Using orion.employee_donations as input, calculate
the difference in each employee’s contribution from one
quarter to the next.
Employee_ID Qtr1 Qtr2 Qtr3 Qtr4
120265 . . . 25
120267 15 15 15 15
120269 20 20 20 20
120270 20 10 5 .
Diff1 = Qtr2 – Qtr1
Diff2 = Qtr3 – Qtr2
Diff3 = Qtr4 – Qtr3
Employee_ID Diff1 Diff2 Diff3
120265 . . .
120267 0 0 0
120269 0 0 0
120270 -10 -5 .
45
45
45
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
p8
46
46
46
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
when i=1
Diff{1}=Contrib{2}-Contrib{1};
Diff1=Qtr2-Qtr1;
47
47 ...
47
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
when i=2
Diff{2}=Contrib{3}-Contrib{2};
Diff2=Qtr3-Qtr2;
48
48 ...
48
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
when i=3
Diff{3}=Contrib{4}-Contrib{3};
Diff3=Qtr4-Qtr3;
49
49
49
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
120265 . . .
120267 0 0 0
120269 0 0 0
120270 -10 -5 .
120271 0 0 0
120272 0 0 0
120275 0 0 0
120660 0 0 0
120662 . . 0
50
50
There are missing values because there were missing values in the intpu data
set. We’ll see later how we can use the SUM function to ignore missing values
when calculating the diffrence of two values.
50
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
51
51
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
Business Scenario
Determine the difference between employee contributions
and the quarterly goals of $10, $20, $20, and $15. Use a
lookup table to store the quarterly goals.
120265 . . . 25
120267 15 15 15 15 Diff1 = Qtr1 – 10
120269 20 20 20 20 Diff2 = Qtr2 – 20
Diff3 = Qtr3 – 20
Diff4 = Qtr4 – 15
Employee_ID Diff1 Diff2 Diff3 Diff4
120265 . . . 10
120267 5 -5 -5 0
120269 10 0 0 5
52
52
52
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
PDV
Target1 Target2 Target3 Target4 Target5
R R R R R
N8 N8 N8 N8 N8
50 100 125 150 200
53
53
(initial-value-list) lists the initial values for the corresponding array elements.
The values for elements can be numbers or character strings. Character strings
must be enclosed in quotation marks. Use commas or spaces to separate values
in the list.
When an initial value list is provided for array elements, the values are
retained. The variables are not read-only – so you can change them, but if you
do not change them, they will have constant values for the life of the DATA
step.
Elements and values are matched by position. If there are more array elements
than initial values, the remaining array elements are assigned missing values
and SAS issues a warning.
53
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial PDV
Employee_
Qtr1 Qtr2 Qtr3 Qtr4
ID
p9
54
54
54
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial PDV
Employee_
Qtr1 Qtr2 Qtr3 Qtr4
ID
55
55 ...
55
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
56
56 ...
56
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
57
57 ...
57
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
58
58 ...
58
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
59
59 ...
Drop flags are set. Notice that variables in the lookup table are dropped.
59
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
R R R R
Diff2 Diff3 Diff4 Goal1 Goal2 Goal3 Goal4 i
60
60 ...
60
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
PDV Is Initialized
data compare(drop=i Goal1-Goal4);
set orion.employee_donations;
array Contrib{4} Qtr1-Qtr4; Initialize PDV
array Diff{4};
array Goal{4} (10,20,20,15);
do i=1 to 4;
Diff{i}=Contrib{i}-Goal{i};
end;
run;
Partial PDV
Employee_
Qtr1 Qtr2 Qtr3 Qtr4 Diff1
ID
. . . . . .
R R R R
Diff2 Diff3 Diff4 Goal1 Goal2 Goal3 Goal4 i
. . . 10 20 20 15 .
61
61
61
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
p10
62
62
You can make the lookup table temporary by using the _temporary_ keyword.
Remove the DROP= option for the table.
Temporary tables are not stored in the PDV – they are stored in memory. The
elements are not named, so you cannot refer to them as Goal1-Goal4. You
must refer to them as Goal{1} through Goal{4}.
Arrays of temporary elements are useful when the only purpose for creating an
array is to perform a calculation. To preserve the result of the calculation,
assign it to a variable.
Note: For a character array, the type and length of elements must be specified
before the _temporary_ keyword.
62
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
120265 . . . 10
120267 5 -5 -5 0
120269 10 0 0 5
120270 10 -10 -15 .
120271 10 0 0 5
63
63
If the quarterly donation was missing, the Difference is missing because the
values are being subtracted.
Like addition, in subtraction if any operands have missing values, the result is
a missing value.
63
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
data compare(drop=i);
set orion.employee_donations;
array Contrib{4} Qtr1-Qtr4;
array Diff{4};
array Goal{4} _temporary_ (10,20,20,15);
do i=1 to 4;
Diff{i}=sum(Contrib{i},-Goal{i});
end;
run;
p11
64
64
Use the SUM function with a negative prefix (or unary minus) on the operand
to be subtracted. Now the missing values are ignored.
64
C o p yr i g ht © 2 0 1 4 , S A S Ins ti tut e Inc . A ll r i g hts r e s e r ve d .
65
65
65
Rotating Data: using arrays
66
Co p y r ig h t © S A S I n s titu te I n c . A ll r ig h ts r es er v ed .
Another reason to use arrays is to rotate data. For this example, we have a table
containing five rows which corresponds to five years and four columns of precipitation
in inches corresponding to four quarters. However, we need the quarter precipitation
values to be in one column so that we can create a bar chart showing average
precipitation by quarter. In the end, we need a table with 20 rows, 4 quarters per each of
the 5 years to feed into our graphing procedure. Notice in the bar chart, the labels at the
end of each bar. The average precipitation for quarter 1 is 7.65 inches, quarter 2 is 6.26
inches, quarter 3 is 7.56 inches, and quarter 4 is 9.12 inches. Remember those values
because we will use them later in another scenario.
Rotating Data: using arrays
do Quarter=1 to 4;
Amount=Q[Quarter];
output;
end;
67 p12
Co p y r ig h t © S A S I n s titu te I n c . A ll r ig h ts r es er v ed .
In order to rotate the data, we can have an array pointing to the four quarter values.
Then we will go through a DO loop four times, each time assigning the appropriate
quarter value to the Precip column. We can’t forget to use the OUTPUT statement so
that each Precip value will be output as its own row.
You might be thinking “Couldn’t I just use PROC TRANSPOSE to rotate this data”
Sure, but the power of the DATA step is that we can rotate and manipulate data at the
same time.
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
68
68
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Business Scenario
Calculate the difference between the current salary of
each employee hired in a specific year and the average
salary of all employees hired that year.
year of hire=????
salary
difference
69
You need to perform this same step for all employees, regardless of their year
of hire. First, you need a table of average salaries for each year of hire.
69
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
70
70
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial [Link]
EmployeeID Salary BirthDate EmployeeHireDate
71
71
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
72
72
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
73
73
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
To calculate the salary differences, you can look up the average salaries from a
table. The key to the lookup is the hire year.
74
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
data compare;
keep EmployeeID YearHired Salary
Average SalaryDif;
format Salary Average SalaryDif dollar12.2;
array yr{1978:2011} Yr1978-Yr2011;
if _n_=1 then set [Link]
(where=(Statistic='AvgSalary'));
set [Link]
(keep=EmployeeID EmployeeHireDate Salary);
YearHired=year(EmployeeHireDate);
Average=yr{YearHired};
SalaryDif=Salary-Average;
run;
p13
75
In this program, the ARRAY statement creates the one-dimensional array yr,
which includes 34 elements, the numeric variables Yr1978 through Yr2011.
Notice that you're not assigning initial values to these array elements as you
did in the previous example. Assigning initial values is appropriate when you
have a small number of values or if the values rarely change. For this task,
you'll initialize the variables with the values from the SalaryStats data set, but
you're only interested in a single row where Statistic equals Avg_Salary.
75
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
data compare;
keep EmployeeID YearHired Salary
Average SalaryDif;
format Salary Average SalaryDif dollar12.2;
array yr{1978:2011} Yr1978-Yr2011;
if _n_=1 then set [Link]
(where=(Statistic='AvgSalary'));
set [Link]
(keep=EmployeeID EmployeeHireDate Salary);
YearHired=year(EmployeeHireDate);
Average=yr{YearHired};
SalaryDif=Salary-Average;
run;
76
Next is the lookup. This assignment statement retrieves the average salary
from the array yr at the appropriate position, and assigns the value to the
variable Average.
Finally, you calculate the salary difference.
76
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial PDV
Salary Average SalaryDif
77 ...
This is processing is at the end of compile but before execution. Notice the
array is set up and the initial values loaded.
77
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Salary
Salary Average
Average SalaryDif D Yr1978
SalaryDif Yr1978 D Yr1979
Yr1979 D Yr1980
Yr1980 D Yr1981
Yr1981 D Yr1982
Yr1982 …
. . . . . . . .
yr{2007} yr{2008} yr{2009} yr{2010} yr{2011}
DYr2007 D
Yr2008 D
Yr2009 D
Yr2010 D
Yr2011
78 ...
78
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Salary
Salary Average
Average SalaryDif D Yr1978
SalaryDif Yr1978 D Yr1979
Yr1979 D Yr1980
Yr1980 D Yr1981
Yr1981 D Yr1982
Yr1982 …
. . . . . . . .
yr{2007} yr{2008} yr{2009} yr{2010} yr{2011}
DYr2007 D
Yr2008 D
Yr2009 D
Yr2010 D
Yr2011 Statistic
79 ...
79
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Salary
Salary Average
Average SalaryDif D Yr1978
SalaryDif Yr1978 D Yr1979
Yr1979 D Yr1980
Yr1980 D Yr1981
Yr1981 D Yr1982
Yr1982 …
. . . . . . . .
yr{2007} yr{2008} yr{2009} yr{2010} yr{2011}
DYr2007 D D D D Employee
Yr2008 Yr2009 Yr2010 Yr2011 Statistic EmployeeIDD
HireDate
80 ...
80
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Salary
Salary Average
Average SalaryDif D Yr1978
SalaryDif Yr1978 D Yr1979
Yr1979 D Yr1980
Yr1980 D Yr1981
Yr1981 D Yr1982
Yr1982 …
. . . . . . . .
yr{2007} yr{2008} yr{2009} yr{2010} yr{2011}
81 ...
81
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Salary
Salary Average
Average SalaryDif D Yr1978
SalaryDif Yr1978 D Yr1979
Yr1979 D Yr1980
Yr1980 D Yr1981
Yr1981 D Yr1982
Yr1982 …
. . . . . . . .
yr{2007} yr{2008} yr{2009} yr{2010} yr{2011}
Employee Year
Yr2007 Yr2008 Yr2009 Yr2010 Yr2011 Statistic EmployeeID _N_
HireDate Hired
82 ...
82
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Salary
Salary Average
Average SalaryDif DD Yr1978
SalaryDif Yr1978 DD Yr1979
Yr1979 DD Yr1980
Yr1980 DD Yr1981
Yr1981 DD Yr1982
Yr1982 …
. . . . . . . .
yr{2007}
yr{2008} yr{2008}
yr{2009} yr{2009}
yr{2010} yr{2010}
yr{2011} yr{2011}
D StatisticEmployeeID Employee
Employee Year
Year D
DDYr2007
Yr2008 D Yr2008
D Yr2009D Yr2009
DYr2010
D Yr2010
DYr2011
D Yr2011
D Statistic EmployeeIDD D _N_
_N_
HireDate Hired
HireDate Hired
. . . . . . . 1
83 ...
83
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Salary
Salary Average
Average SalaryDif DD Yr1978
SalaryDif Yr1978 DD Yr1979
Yr1979 DD Yr1980
Yr1980 DD Yr1981
Yr1981 DD Yr1982
Yr1982 …
.. . . . . . . .
yr{2007}
yr{2008} yr{2008}
yr{2009}yr{2009}
yr{2010}yr{2010}
yr{2011}
yr{2011}
D StatisticEmployeeID Employee
Employee Year
Year D
DDYr2007
Yr2008 D Yr2008
D Yr2009D Yr2009
DYr2010
D Yr2010
DYr2011
D Yr2011
D Statistic EmployeeIDD D _N_
_N_
HireDate Hired
HireDate Hired
. . . . . . . . . . . . . .. 1
84 ...
84
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Salary
Salary Average
Average SalaryDif D Yr1978
SalaryDif Yr1978 D Yr1979
Yr1979 D Yr1980
Yr1980 D Yr1981
Yr1981 D Yr1982
Yr1982 …
yr{2007}
yr{2008} yr{2008}
yr{2009}yr{2009}
yr{2010}yr{2010}
yr{2011}
yr{2011}
D StatisticEmployeeID Employee
Employee Year
Year D
D Yr2007
Yr2008 D Yr2008
Yr2009D Yr2009
Yr2010D Yr201 D Yr2011
Yr2011 Statistic EmployeeID D _N_
_N_
HireDate Hired
HireDate Hired
35082.5
29904.44 29904.44
30576.36 30576.36
27883.7127883.71
28861.67
28861.67
AvgSalary
AvgSalary . . . . .. 1
85 ...
85
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Salary
Salary Average
Average SalaryDif DD Yr1978
SalaryDif Yr1978 DD Yr1979
Yr1979 DD Yr1980
Yr1980 DD Yr1981
Yr1981 DD Yr1982
Yr1982 …
163040
163040 . . 39243.61 33037.5 39171.67 34170 37506.25
yr{2007}
yr{2008} yr{2008}
yr{2009}yr{2009}
yr{2010}yr{2010}
yr{2011}
yr{2011}
D StatisticEmployeeID Employee
Employee Year
Year D
DDYr2007
Yr2008 D Yr2008
D Yr2009D Yr2009
DYr2010
D Yr2010
DYr2011
D Yr2011
D Statistic EmployeeIDD D _N_
_N_
HireDate Hired
HireDate Hired
35082.5
29904.44 29904.44
30576.36 30576.36
27883.7127883.71
28861.67
28861.67
AvgSalary
AvgSalary 120101
120101 01JUL2007
01JUL2007 .. 1
86 ...
86
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
87 ...
87
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
88 ...
88
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
89 ...
89
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
90 ...
90
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
91 ...
91
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
92
92
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Resulting Data
proc print data=compare(obs=5);
var EmployeeID YearHired Salary Average SalaryDif;
title 'Using One Dimensional Arrays';
run;
Year
Obs EmployeeID Hired Salary Average SalaryDif
93
93
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
94
94
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
2 One-Dimensional Arrays
3 Two-Dimensional Arrays
95
95
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Objectives
◼ Define a two-dimensional array.
◼ Explain the differences between a one-dimensional
array and a two-dimensional array.
96
96
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Two-Dimensional Arrays
Two-dimensional arrays has a row dimension and column
dimension.
Column
1,1 1,2
Row
2,1 2,2
97
97
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
98
98
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
99
99
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
2 One-Dimensional Arrays
3 Two-dimensional Arrays
100
100
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Objectives
◼ Load a two-dimensional array from a SAS data set.
◼ Identify the advantages of an array as a lookup table.
101
101
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Business Scenario
Compare the actual profit values for each Orion Star
company to the budgeted profit values.
102
102
102
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Business Scenario
This time, the budget values are stored in the
[Link] data set.
Partial [Link]
Month Yr2007 Yr2008 Yr2009 Yr2010 Yr2011
1 $1,590,000 $1,880,000 $2,300,000 $1,960,000 $1,970,000
2 $1,290,000 $1,550,000 $1,830,000 $1,480,000 $1,640,000
3 $1,160,000 $1,380,000 $1,640,000 $1,410,000 $1,440,000
4 $1,710,000 $2,100,000 $2,420,000 $2,130,000 $2,270,000
5 $1,990,000 $2,350,000 $2,840,000 $2,480,000 $2,670,000
.
.
.
12 $2,870,000 $3,120,000 $3,760,000 $3,210,000 $4,370,000
103
103
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
104
The main reason for using a SAS data set to load the array is to avoid having
to type them. Array values should be read from a SAS data set when any of the
following conditions exist:
• There are too many values to initialize easily in the array.
• The values change frequently.
• The same values are used in many programs.
104
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
105
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial [Link]
Month Yr2007 Yr2008 Yr2009 Yr2010 Yr2011
1 $1,590,000 $1,880,000 $2,300,000 $1,960,000 $1,970,000
2 $1,290,000 $1,550,000 $1,830,000 $1,480,000 $1,640,000
3 $1,160,000 $1,380,000 $1,640,000 $1,410,000 $1,440,000
4 $1,710,000 $2,100,000 $2,420,000 $2,130,000 $2,270,000
5 $1,990,000 $2,350,000 $2,840,000 $2,480,000 $2,670,000
106
106
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
107
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial [Link]
Month Yr2007 Yr2008 Yr2009 Yr2010 Yr2011
1 $1,590,000 $1,880,000 $2,300,000 $1,960,000 $1,970,000
2 $1,290,000 $1,550,000 $1,830,000 $1,480,000 $1,640,000
3 $1,160,000 $1,380,000 $1,640,000 $1,410,000 $1,440,000
4 $1,710,000 $2,100,000 $2,420,000 $2,130,000 $2,270,000
5 $1,990,000 $2,350,000 $2,840,000 $2,480,000 $2,670,000
B{1,2007}
108
This time we've got the array data in a data set where each observation is a
month and the YR variables represent years.
108
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
. . .
This is where processing is at the end of compilation. Notice that the array B
has no values in it, but it is temporary and not part of the PDV.
109
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
. . .
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonthD DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 D DYr2010
Yr2010 DDYr2011
Yr2011 DD Yr Company
11 . . .. .. .. .. .. ..
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
110 ...
The ARRAY statement is not executable, that's why the highlighting skips to
the IF statement. Since the IF statement is true, the DO statement executes and
I is set to 1.
110
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
. . .
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonthD DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 D DYr2010
Yr2010 DDYr2011
Yr2011 DD Yr Company
Company
11 11 1590000
1590000 1880000
1880000 2300000
2300000 1960000
1960000 1970000
1970000 ..
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
111 ...
The first observation of the data set [Link] is loaded into the PDV.
111
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
. . .
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
11 11 1590000
1590000 1880000
1880000 2300000
2300000 1960000
1960000 1970000 2007
2007
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
112 ...
Yr is now 2007.
112
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
. . .
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
11 11 1590000
1590000 1880000
1880000 2300000
2300000 1960000
1960000 1970000 2007
2007
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
113 ...
113
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
1590000 . . .
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
11 11 1590000
1590000 1880000
1880000 2300000
2300000 1960000
1960000 1970000 2007
2007
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
114 ...
The assignment statement uses values from the PDV to fill in the appropriate
array element with the value referenced by the array TMP.
114
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
1590000 . . .
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
11 11 1590000
1590000 1880000
1880000 2300000
2300000 1960000
1960000 1970000 2008
2008
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
115 ...
Yr increments to 2008.
115
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
1590000 . . .
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
11 11 1590000
1590000 1880000
1880000 2300000
2300000 1960000
1960000 1970000 2008
2008
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
116 ...
116
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
1590000 1880000 . . .
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
11 11 1590000
1590000 1880000
1880000 2300000
2300000 1960000
1960000 1970000 2008
2008
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
117 ...
117
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
12 11 1590000
1590000 1880000
1880000 2300000
2300000 1960000
1960000 1970000 2012
2012
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
118 ...
118
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
22 11 1590000
1590000 1880000
1880000 2300000
2300000 1960000
1960000 1970000 2012
2012
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
119 ...
119
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
22 22 1290000
1290000 1550000
1550000 1830000
1830000 1480000
1480000 1640000 2012
2012
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
120 ...
120
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
22 22 1290000
1290000 1550000
1550000 1830000
1830000 1480000
1480000 1640000 2007
2007
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
121 ...
121
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
PartialPDV
Partial PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonth D DYr2007
DMonD Month
DMon Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010 DDYr2011
Yr2010
DD Yr2011 DD Yr Company
Company
22 22 1290000
1290000 1550000
1550000 1830000
1830000 1480000
1480000 1640000 2007
2007
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
122 ...
122
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
22 22 1290000
1290000 1550000
1550000 1830000
1830000 1480000
1480000 1640000 2007
2007
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
123 ...
123
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
22 22 1290000
1290000 1550000
1550000 1830000
1830000 1480000
1480000 1640000 2008
2008
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
124 ...
124
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
22 22 1290000
1290000 1550000
1550000 1830000
1830000 1480000
1480000 1640000 2008
2008
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
125 ...
125
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
22 22 1290000
1290000 1550000
1550000 1830000
1830000 1480000
1480000 1640000 2009
2009
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
126 ...
126
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
1212 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 2010
2010
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
127 ...
127
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
1212 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 2010
2010
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
128 ...
128
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
1212 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 2010
2010
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
129 ...
129
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
1212 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 2011
2011
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
130 ...
130
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
1212 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 2011
2011
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
131 ...
131
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
1212 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 2011
2011
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
132 ...
132
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
1212 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 2012
2012
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
133 ...
133
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
1313 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 2012
2012
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
134 ...
134
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
1313 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 2012
2012
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
. . . . . . . . 1
135 ...
135
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
1313 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 2012
2012 Logistics
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
07M01 457809 210914 127525 119370 . . . 1
136 ...
136
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
1313 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 2012
2012 Logistics
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
07M01 457809 210914 127525 119370 2007 1 . 1
137 ...
137
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
1313 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 2012
2012 Logistics
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
07M01 457809 210914 127525 119370 2007 1 . 1
138 ...
138
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
1313 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 2012
2012 Logistics
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
07M01 457809 210914 127525 119370 2007 1 1590000 1
139 ...
139
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
1313 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 2012
2012 Logistics
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
07M01 457809 210914 127525 119370 2007 1 1590000 1
140 ...
140
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonDDMonth
DDMon DMonth DD DYr2007
Yr2007 DDDYr2008
Yr2008 DDDYr2009
Yr2009 Yr2010
DDDYr2010 DDDYr2011
Yr2011 DD Yr Company
Company
. . 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 2012. Logistics
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
07M01 457809 210914 127525 119370 . . . 2
141 ...
141
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Execution False
data budgetamt;
Partial [Link] drop Yr2007-Yr2011 Month Mon Yr Y M;
array B{12,2007:2011} _temporary_;
Company YYMM Sales ... if _N_=1 then do Mon=1 to 12;
set [Link];
Logistics 07M01 457809 ... array cols{2007:2011} Yr2007-Yr2011;
do Yr=2007 to 2011;
Logistics 07M02 325138 ... B{Mon,Yr}=cols{Yr};
end;
Logistics 07M03 276805 ... end;
set [Link](where=(Sales ne .));
Logistics 07M04 558806 ... Y=year(YYMM);
. . . M=month(YYMM);
... BudgetAmt=B{M,Y};
. . . run;
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
. . 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 2012. Logistics
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
07M02 457809 210914 127525 119370 . . . 2
142 ...
The IF/THEN DO group does not execute again because _N_ is 2. That is
important to prevent SAS from hitting the end of file marker on [Link]
and ending the DATA step.
142
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
. . 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 .. Logistics
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
07M02 325138 149718 127525 47895 . . . 2
143 ...
The second SET statement executes again, and the second observation of
[Link] is copied into the PDV.
143
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
. . 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 .. Logistics
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
07M02 325138 149718 127525 47895 2007 2 1290000 2
144 ...
144
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
. . 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 .. Logistics
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
07M02 325138 149718 127525 47895 2007 2 1290000 2
145 ...
145
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
Partial
PartialPDV
PDV cols{2007}
tmp{2007} cols{2008}
tmp{2008} cols{2009} tmp{2010} cols{2011}
tmp{2009} cols{2010} tmp{2011}
DMonD Month
DMon DMonth D DYr2007
Yr2007 D DYr2008
Yr2008 D DYr2009
Yr2009 Yr2010
DDYr2010 DDYr2011
Yr2011 DD Yr Company
Company
. . 1212 2870000
2870000 3120000
3120000 3760000
3760000 3210000
3210000 4370000 .. Logistics
Budget
YYMM Sales Cost Salaries Profit D Y D M D _N_
Amt
07M02 325138 149718 127525 47895 2007 2 1290000 2
146
And execution continues until the end of the file marker in [Link].
146
C opy r i ght © 2014, SAS I nsti tute I nc . All r i ghts r eser ved.
147
147