proc format

advertisement
Arrays
An array is a way of easily referencing a collection of variables.
Suppose we have variables Q1 Q2 Q3 Q4 and Q5.
So, Q1 constains responses to the first question on a questionnaire.
Q2 contains responses to the 2nd question
Q3 the third
Q4 the fourth
Q5 the fifth
Suppose all the items are scores on a -2 to +2 scale, and we wish to rescore them as 1 to 5
items. That means, we’d like to add 3 to each value so that -2 would become 1, -1 would
become 2, 0 would become 3, and so forth.
SAS Statements
Q1 = Q1 + 3;
Q2 = Q2 + 3;
Q3 = Q3 + 3;
Q4 = Q4 + 3;
Q5 = Q5 + 3;
Obviously, if there were 50 items or 100 items, this would get quite tedious.
We need a way to make it easier to reference collections of variables.
Array: A way of referencing 2 or more variables using a single name + a location.
Q(I): Q is the name of the collection of variables.
I is the location of the specific variable you’re referencing.
Q(1): the first, e.g., Q1
Q(2): the second, e.g., Q2
Doing the recode
DO I=1 TO 5;
Q(I) = Q(I) + 3;
END;
Arrays must be declared using the ARRAY statement.
ARRAY name(size) variables synonymous with the array elements
ARRAY X(10) X1-X10;
ARRAY X(100) Y1-Y100;
ARRAY VAR(50) X1-X10 Y1-Y40;
ARRAY VAR(5) A B C D E;
Aside: The use of arrays here is different from that in other programming languages.
Here, arrays are alternative names for variables.
In other languages, arrays are separate from other variables.
Referencing arrays
ARRAY X(10) X1-X10;
X(1)=5;
X(6)=4;
More often in DO loops
DO I=1 to 10;
X(I)=I**2;
END;
Scoring the Big 5
Suppose the Big 5 is arranged so that every 5th item is from the same scale.
1st, 6th, 11th, 16th, are E
2nd, 7th, etc are A.
3rd, 8th, 13th, are C.
4th, 9th, 14th, are S.
5th, 10th, 15th are O.
data Big5;
array big(50) x1-x50;
input (x1-x50) (1-50);
e=0; a=0; c=0; s=0; o=0;
do i=1 to 50 by 5;
e=e+x(i);
a=a+x(i+1);
c=c+x(i+2);
s=s+x(i+3);
o=o+x(i+4c=c+x(i+2);
s=s+x(i+3);
o=o+x(i+4);
end;
This doesn’t account for
negatively worded items.
Need an elegant way to
incorporate them.
Two dimension arrays.
ARRAY X(100,2) X1-X200;
Let’s agree
1) Two-dimension arrays correspond to two-dimension tables.
2) X(I,J) I refers to the row in which the value resides. J refers to the column.
3) Oftentimes, two-dimensional arrays are used to represent two or more groups of
scores, e.g., pairs or triplets.
Let’s agree to let J represent the column containing the set of scores, as in the SPSS data
editor window.
4) Given the above, let’s agree to let I refer to the person/case/participant/subject.
X(8,4): Value in the 8th row and 4th
column = 12
Computing the sum of products of values in two columns.
data sumprods;
array x(10,4) x1-x40;
input x1-x40;
** sum of products of values in columns 2 and 4;
sumprod = 0;
do i=1 to 10;
sumprod = sumprod + x(I,1)*x(I,2);
end;
put sumprod;
datalines;
1 2 3 4
5 6 7 8
9 10 11 12
1 2 3 4
5 6 7 8
9 10 11 12
1
5
9
1
5
9
1
5
9
1
5
9
1
5
9
1
5
9
1
5
9
1
5
9
;
2 3 4
6 7 8
10 11
2 3 4
6 7 8
10 11
2 3 4
6 7 8
10 11
2 3 4
6 7 8
10 11
2 3 4
6 7 8
10 11
2 3 4
6 7 8
10 11
2 3 4
6 7 8
10 11
2 3 4
6 7 8
10 11
12
12
12
12
12
12
12
12
47
48
49
50
51
IF K1 <6 then SCALE1 = .;
if K2 < 3 THEN SCLAE2 = .;
IF K3 < 4 THEN SCALE3 = .;
PROC PRINT;
RUN;
Each of 5 raters rates 4 stimuli.
This program computes row totals (e.g., rater totals) and column totals (e.g., stimulus
totals).
Download