14.03.2014 Views

Scripting Guide - SAS

Scripting Guide - SAS

Scripting Guide - SAS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7 Data Structures 151<br />

Matrices<br />

• The returned values are always 1 or greater. If the element in B is smaller than all of the elements in A,<br />

then the function returns a value of 1. If the element in B is greater than all of the elements in A, then<br />

the function returns n, where n is the number of elements in A.<br />

Ranking and Sorting<br />

The Rank() function returns the positions of the numbers in a vector or list, as if the numbers were sorted<br />

from lowest to highest.<br />

E=[1 -2 3 -4 0 5 1 8 -7];<br />

R=Rank(E);<br />

[9,4,2,5,7,1,3,6,8]<br />

If E were sorted from lowest to highest, the first number would be -7. The position of -7 in E is 9.<br />

The original matrix E can then be sorted using the matrix R as subscripts to E.<br />

sortedE=E[R];<br />

[-7,-4,-2,0,1,1,3,5,8]<br />

The Ranking Tie() function returns ranks for the values in a vector or list, with ranks for ties averaged.<br />

Similarly, Ranking() returns ranks for the values in a vector or list, but the ties are ranked arbitrarily.<br />

E=[1 -2 3 -4 0 5 1 8 -7];<br />

Ranking Tie (E);<br />

[5.5, 3, 7, 2, 4, 8, 5.5, 9, 1]<br />

E=[1 -2 3 -4 0 5 1 8 -7];<br />

Ranking (E);<br />

[5, 3, 7, 2, 4, 8, 6, 9, 1]<br />

The Sort Ascending() and Sort Descending() functions sort vectors.<br />

E=[1 -2 3 -4 0 5 1 8 -7];<br />

Sort Ascending (E);<br />

[-7 -4 -2 0 1 1 3 5 8]<br />

E=[1 -2 3 -4 0 5 1 8 -7];<br />

Sort Descending (E);<br />

[8 5 3 1 1 0 -2 -4 -7]<br />

If the argument is not a vector or list, an error message is generated.<br />

Special Matrices<br />

Construct an Identity Matrix<br />

The Identity() function constructs an identity matrix of the dimension that you specify. An identity<br />

matrix is a square matrix of zeros except for a diagonal of ones. The only argument specifies the dimension.<br />

Identity(3);<br />

[1 0 0,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!