0% found this document useful (0 votes)
6 views45 pages

6arrays &loops

The document provides an overview of SAS arrays, detailing their definition, syntax, and usage within data steps. It explains how to create and manipulate arrays, including one-dimensional, two-dimensional, and three-dimensional arrays, as well as the use of loops for processing array elements. Additionally, it covers the DO WHILE and DO UNTIL statements for iterative operations on arrays.

Uploaded by

Arijit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views45 pages

6arrays &loops

The document provides an overview of SAS arrays, detailing their definition, syntax, and usage within data steps. It explains how to create and manipulate arrays, including one-dimensional, two-dimensional, and three-dimensional arrays, as well as the use of loops for processing array elements. Additionally, it covers the DO WHILE and DO UNTIL statements for iterative operations on arrays.

Uploaded by

Arijit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 45

ARRAYS

ARRAYS & LOOPS


• A SAS array
• SAS array is nothing more than a collection of
variables (of the same type), in which each
variable can be identified by referring to the
array and, by means of an index, to the location
of the variable within the array.
• SAS arrays are defined using the ARRAY
statement, and are only valid within the data
step in which they are defined.
• The syntax for the array statement is:

• ARRAY array-name {subscript} <$> < length >


• << array-elements > <( initial-values )>>
ARRAYS & LOOPS
• Why use SAS arrays?
• • repeat an action or set of
actions on each of a group of
variables
• • write shorter programs
• • restructure a SAS data set
to change the unit of observation
ARRAYS & LOOPS(#2)
• ARRAY array-name {subscript} <$> < length >
• << array-elements > <( initial-values )>>
• { subscript } is the dimension (possibly
multiple) of the array, and can be omitted or
specified as {*} in which case SAS infers the
dimension from the number of array elements.
• < array-elements > is the list of array elements
(variables) which can be omitted if the
dimension is given, in which case SAS creates
variables called array-name1 to array-name{n}
where {n} is the dimension of the array. For
example:
• array wt {50};
• will cause the variables wt1-wt50 to be created.
ARRAYS & LOOPS(#3)
• Another Quick Example
• array pop(1:5) ga sc nc va wv;
• array name is "pop"
• the sequence of variable names ga
sc nc va wv is the "array list"
• the variables in the array list are the
"array elements"
• each array element in this example
has a position number in the array list
from 1 to 5
ARRAYS & LOOPS (#4)
• Convenience of arrays
• . two ways to refer to variables which
are array elements
• . variable name
• . array name with subscript pointing to
position of variable in the array list
• . pop(3) refers to the variable in
position # 3 in the array pop (nc in above
example)
ARRAYS & LOOPS (#5)
• Subscripts can be
• • constants
• • variables
• • expressions
• Array elements
• • must all be of same type (numeric
or character)
• • can be variables which exist or
which will be created in the data step
ARRAYS & LOOPS (#6)
• array x(1:6) a b c d e f; x(5) same as e
• array x(0:5) a b c d e f; x(4) same as e
• array quiz(20) q1-q20; equivalent array
declarations
• array quiz(1:20) q1-q20; quiz(4) same as q4
• array quiz(*) q1-q20; subscript lower
bound=1, upper bound=20
• array quiz(20); SAS creates quiz1-
quiz20 as array elements
• array color(1:3) $ 1 character array,
elements have length=1
character
• red blue green; color(2) same as blue
• array pop(1:5) yr95-yr99; pop(2) same as yr96
ARRAYS & LOOPS (#7)
• array quiz(*) q1-q20; subscript lower bound=1,
upper bound=20
• array quiz(20); SAS creates quiz1-quiz20
as array elements
• ARRAY LOG_INC{3} INCOME1 INCOME2 INCOME3 (0 0
0); will initialize the values
• array color(1:3) $ 1 character array, elements
have length=1 character
• red blue green; color(2) same as blue
• array pop(1:5) yr95-yr99; pop(2) same as yr96
• array pop(95:99) yr95-yr99; pop(96) same as yr96
• array pop(1:5) yr95-yr99; pop(2) same as yr96
• array pop(95:99) yr95-yr99; pop(96) same as yr96
• array x(*) _numeric_; all numeric variables on
the observation
• array y(*) _character_; all character variables on
the observation
• array z(*) _all_; all variables on the
observation
ARRAYS & LOOPS (#8)
Two-dimensional array declarations
array quiz(1:4,1:5) q1-q20; picture 4 rows, 5 columns:
q1 q2 q3 q4 q5
q6 q7 q8 q9 q10
q11 q12 q13 q14 q15
q16 q17 q18 q19 q20

q(2,4) same as q9

array pop(1:3,98:99) nc98 nc99 va98 va99 sc98 sc99; picture 3


rows, 2 columns:
nc98 nc99
va98 va99
sc98 sc99

pop(3,99) same as sc99


ARRAYS & LOOPS (#9)
1. Recode the set of variables A B C D
E F G in the same way: if the
variable has a value of 99 recode it
to SAS missing.
DATA Radhika;
INPUT A B C D E F G;
ARRAY V(7) A B C D E F G;
DO K=1 TO 7;
IF V(K)= 99 THEN V(K)=.;
END;
CARDS;
45 78 89 99 34 45 90
99 78 54 34 45 66 99
;
RUN;
PROC PRINT; RUN;
ARRAYS & LOOPS (#10)
ARRAYS & LOOPS (#11)
• ANOTHER EXAMPLE THAT SHOWS
ARRAYS ;DO LOOPS; WITHOUT I/P

• DATA IN(DROP=I);
• ARRAY X (*) X1-X10;
• DO I=1 TO 10;
• X(I)=I;
• END;
• RUN;
• TITLE 'THE DATA AS ONE OBSERVATION';
• PROC PRINT; RUN;
ARRAYS & LOOPS (#13)
DATA IN(DROP=I);
ARRAY X (*) X1-X10;
ARRAY Y (*) Y1-Y10;
• DO I=1 TO 10;
• Y(I) = I * 2; X(I)=I;
END;
RUN;
• TITLE 'THE DATA AS ONE OBSERVATION';
• PROC PRINT;
• RUN;
ARRAYS & LOOPS (#14)
ARRAYS & LOOPS (#15)
• DATA IN(DROP=I);
• 12 ARRAY X (*) X1-X10;
• 13 ARRAY Y (*) Y1-Y9;
• 14 DO I=1 TO 10;
• 16
• 15 Y(I) = I * 2;
• 17 X(I)=I;
• 18 END;
• 19
• 20 RUN;
• ERROR: Array subscript out of range at line 16
column 6.
• X1=1 X2=2 X3=3 X4=4 X5=5 X6=6 X7=7 X8=8 X9=9 X10=. Y1=2 Y2=4 Y3=6
Y4=8 Y5=10 Y6=12 Y7=14 Y8=16 Y9=18 I=10 _ERROR_=1 _N_=1
• NOTE: The SAS System stopped processing this step because of errors.
• WARNING: The data set WORK.IN may be incomplete. When this step was
stopped there were 0 observations and 19 variables.
• WARNING: Data set WORK.IN was not replaced because this step was
stopped.
ARRAYS & LOOPS (#16)
• 2. Each observation of your data set has five variables SEX1 SEX2
SEX3 SEX4 SEX5 which give the sex (1=male, 2=female) of up to 5
persons. You want to count the number of males (MALES) and the
number of females (FEMALES) on each observation.
• Data Another_Array_Example;
• Input Sex1-Sex5;
• array sex(1:5) sex1-sex5;
• males=0; females=0;
• do i=1 to 5;
• if sex(i)=1 then males=males+1;
• else if sex(i)=2 then females=females+1;
• end;
• Cards;
• 12112
• 21121
• ;
• Proc Print; Run;
ARRAYS & LOOPS (#17)
ARRAYS & LOOPS (#18)
3. Recode all numeric variables in your data set as follows:
if a variable has a value of 98 or 99 recode it to SAS
missing. Using * and _NUMERIC_ and DIM

• DATA SHOW_USING_DIM;
• INPUT SEX1-SEX5;
• ARRAY MYARRAY(*) _NUMERIC_;
• DO I=1 TO DIM(MYARRAY);
• IF MYARRAY(I)=98 OR MYARRAY(I)=99 THEN
MYARRAY(I)=.;
• END;
• CARDS;
• 45 78 89 99 34 45 90
• 99 78 54 34 45 66 99
• ;
ARRAYS & LOOPS (#19)
DATA SHOW_TWO_DIMENSIONAL_ARRAY;
• array temprg{2,5} c1t1-c1t5 c2t1-c2t5;
• input c1t1-c1t5 / c2t1-c2t5;
• do i=1 to 2;
• do j=1 to 5;
• temprg{i,j}= temprg{i,j}; /* temprg{i,j}= temprg{i,j} * 2 */
• end;
• end;
• datalines;
• 89.5 65.4 75.3 77.7 89.3
• 73.7 87.3 89.9 98.2 35.6
• 75.8 82.1 98.2 93.5 67.7
• 101.3 86.5 59.2 35.6 75.7
• ;
• Proc print;
ARRAYS & LOOPS (#20)
ARRAYS & LOOPS (#21)
• Three Dimensional Arrays
• DATA THREE_DIMENSIONAL;
• INPUT X1-X8;
• ARRAY THREE_D(2,2,2) X1-X8; /* X1-X8 OR _NUMERIC_ */
• DO I=1 TO 2;
• DO J=1 TO 2;
• DO K=1 TO 2;
• THREE_D(I,J,K) = THREE_D(I,J,K) * 2;
• END;
• END;
• END;
• CARDS;
• 10 20 30 40 50 60 70 80
• 11 22 33 44 55 66 77 88
• 19 29 39 49 59 69 79 89
• ;
• RUN;
• PROC PRINT;
• RUN;
ARRAYS & LOOPS (#22)
ARRAYS & LOOPS (#23)
• DO WHILE Statement
• You can execute the statements in a DO group
repetitively while a condition holds using the DO
WHILE statement.
• The form of the Do While statement is
• Do While(Expression);
• Sas Statements;
• End;

• Where EXPRESSION: is any expression. The


expression is evaluated at the top of the loop before
the statements in the DO group are executed.
ARRAYS & LOOPS (#24)
• These statements repeat the loop as long as long as N is less than 5.
There are 5 iterations in(‘0,1,2,3,4).

• DO WHILE
• DATA DOWHILE_USING;
• N=0;
• DO WHILE (N LT 5); /* N < 5 */
• OUTPUT;
• N+1;
• END;
RUN;
• PROC PRINT;
• RUN;
ARRAYS & LOOPS (#25)
DATA TEST_DO_LOOP;
• INPUT SCORE1-SCORE5;
• ARRAY S{5} SCORE1-SCORE5;
• N=1;
• DO WHILE( N LE 5);
• S{N}=S{N}*10;
• N+1;
• END;
• CARDS;
• 10 20 30 40 50
• 11 22 32 42 52
• ;
• Run;
PROC PRINT;
RUN;
ARRAYS & LOOPS (#26)
WITHOUT RUNNING A PROGRAM, WHAT WILL BE THE OUTPUT OF
THE PROGRAM (QUESTION TO STUDENTS) ??? THINKKKKKKKKK
DATA TEST;
• INPUT SCORE1-SCORE5;
• ARRAY S{5} SCORE1-SCORE5;
• N=1;
• DO WHILE( N LT 5);
• S{N}=S{N}*10;
• N+1;
• END;
• CARDS;
• 10 20 30 40 50
• 11 22 32 42 52
• ;
• RUN;
PROC PRINT;
RUN;
ARRAYS & LOOPS (#27)
ARRAYS & LOOPS (#28)
• DO UNTIL statement
• Do Until statement, like the Do While statement, executes the
statements in a Do loop conditionally. The Do Until evaluates the
condition at the bottom of the loop rather than at the Top (as
Does While. Thus the statements b/n DO and END are always
executed at least one time. The form of the DO UNTIL statement
is
• DO UNTIL(Expression); where expression is any expression.

• DATA DO_UNTIL;
• N=0;
• DO UNTIL (N>=5);
• OUTPUT;
• N+1;
• END;
• RUN;
• PROC PRINT;
• RUN;
ARRAYS & LOOPS (#29)
• Another Example where N=6 and then starts the
loop.
• DATA Another_Example_Do_Until;
• N=6;
• DO UNTIL (N>=5);
• OUTPUT;
• N+1;
• END;
• RUN;
• PROC PRINT;
• RUN;
ARRAYS & LOOPS (#30)
• DATA TEST;
• INPUT SCORE1-SCORE5;
• ARRAY S{5} SCORE1-SCORE5;
• N=1;
• DO UNTIL( N GT 5);
• S{N}=S{N}*10;
• N+1;
• END;
• CARDS;
• 10 20 30 40 50
• 11 22 32 42 52
• ;
• RUN;
• PROC PRINT;
• RUN;
ARRAYS & LOOPS (#31)
• WHAT HAPPENS TO O/P IF USE “ N GE 5”

• DATA TEST;
• INPUT SCORE1-SCORE5;
• ARRAY S{5} SCORE1-SCORE5;
• N=1;
• DO UNTIL( N GE 5);
• S{N}=S{N}*10;
• N+1;
• END;
• CARDS;
• 10 20 30 40 50
• 11 22 32 42 52
• ;
• RUN;

• PROC PRINT; RUN;


ARRAYS & LOOPS (#32)
ARRAYS & LOOPS (#33)
ARRAYS USING {} INSTEAD OF USING ()
• DATA CHECKS;
• INPUT X1-X5 Y;
• ARRAY T{5} X1-X5;
• I = 1;
• DO WHILE(T{I} < Y );
• PUT T{I} = Y=;
• I = I + 1;
• END;
• CARDS;
• 123453
• 024686
• ;
RUN;
PROC PRINT; RUN;
In the SAS log, you will see values for T{I},Y bcz of PUT
statement(When you use PUT= it will show the result(s) only in
the SAS LOG)
ARRAYS & LOOPS (#34)
ARRAYS & LOOPS (#35)
• IMPICIT DO LOOPS– ARRAYS
• Replace where ever you see 99 by 100

• DATA CHECKS;
• INPUT D1-D7;
• ARRAY DAYS(INDX) D1-D7;
• DO INDX = 1 TO 7;
• IF DAYS = 99 THEN DAYS=100;
• END;
• CARDS;
• 99 100 200 99 100 99 34
• 34 100 99 45 500 99 23
• ;
• RUN;

• PROC PRINT;
• RUN;
ARRAYS & LOOPS (#36)
• LOOPS USING DO OVER
DATA TEST;
INPUT SCORE1-SCORE5;
ARRAY S SCORE1-SCORE5;
DO OVER S;
S=S*10;
END;
CARDS;
10 20 30 40 50
11 22 32 42 52
;
RUN;
PROC PRINT;
RUN;
ARRAYS & LOOPS (#37)
• LOOPS USING DO OVER
ARRAYS & LOOPS (#38)
DATA CHECKS; DATA CHECKS;
DO I= 1 TO 10; DO I= 1 TO 10 BY 2;
J=I+1; J=I+1;
OUTPUT; OUTPUT;
END; END;
RUN; RUN;
PROC PRINT; RUN;
PROC PRINT;

Here the loops runs 4 times for each DATA CHECKS;


2,3,5,7 value;;;;; DO I= 'JAN','FEB','MAR';
DATA CHECKS; J=I || 'MONTH';
DO I= 2,3,5,7; OUTPUT;
J=I+1; END;
OUTPUT; RUN;
END; PROC PRINT;
RUN; RUN;
PROC PRINT; RUN;
ARRAYS & LOOPS (#39)
BY using _Temporary_ you can supply the Negative Increment
data values in the array statement itself.
DATA TEMPS; DATA CHECKS;
ARRAY TEMPRG{3} _TEMPORARY_ DO I= 10 TO 1 BY -1;
(20,30,40);
J=I+1;
DO I=1 TO 3;
K=TEMPRG{I} * 2; OUTPUT;
OUTPUT; END;
END; RUN;
RUN;
PROC PRINT; RUN;
PROC PRINT; RUN;

DATA CHECKS; Do and While in the same statement;


DO I= 2 TO 8 BY 2,11,13 TO 16;
DATA CHECKS;
J=I+1;
MONTH='JAN';
OUTPUT; DO I= 10 TO 1 BY -1 WHILE(MONTH='JAN');
END; J=I+1;
RUN; IF J=6 THEN MONTH='FEB';
PROC PRINT; OUTPUT;
RUN; END;
RUN;
PROC PRINT; RUN;
ARRAYS & LOOPS (#40)
ARRAYS & LOOPS (#41)
DATA CHECKS;
DO I= .1 TO .9 BY .1;
J=I+1;
OUTPUT;
END;
RUN;
PROC PRINT;
RUN;

DATA CHECKS; The following are the valid Do loops , you may
J=0; want to use inside the program.
DO YEAR=1946 TO 1949; Do I = 1 to N;
J=J+YEAR; Do I = N to 1 by –1;
OUTPUT;
Do I=K+1 TO N-1;
END;
RUN;
Do I=1 to K-1, K+1 to N;
PROC PRINT; Do I=’Saturday’, ‘Sunday’,’Monday’;
RUN; Do I=’01jan85’d ,’06Apr98’d;
ARRAYS & LOOPS (#42)
• This very important example that shows ANY CHANGES
MADE TO THE UPPER BOUND or INCREMENT within
the DO group do not effect the no of iterations. In this
example my Upper bound is 5 but inside the loop I have set to
20. Inspite of being it set to 20 it will not come out of the loop.
• It just overlooks the value for considering the UPPERBOUND.
However if you change the INDEX value(In this case I) then loop
gets EFFECTED. Shows in the next example.

• DATA CHECKS;
• K=5;
• DO I= 1 TO K;
• K=20;
• OUTPUT;
• END;
• run ;
• PROC PRINT; RUN;
ARRAYS & LOOPS (#43)
• 11. CHANGE THE INDEX value thereby it effects the Loop.

• DATA CHECKS;
• K=5;
• DO I= 1 TO K;
• I=6;
• OUTPUT;
• END;
• RUN ;
• PROC PRINT;
• RUN;

ARRAYS & LOOPS (#44)
DIM FUNCTION: Dim function is really useful when you declare an array with an
unknown number of elements.

• DATA IN(DROP=I);
• ARRAY X (*) X1-X10;
• DO I=1 TO DIM(X);
• X(I)=I;
• END;
• RUN;
• TITLE 'THE DATA HAS ONE OBSERVATION';

• You can write this way also; I can also use Curling braces while declaring and
referencing array elements , However I can’t use Curling braces while using DIM
function.

• DATA IN(DROP=I);
• ARRAY X {*} X1-X10;
• DO I=1 TO DIM(X);
• X{I}=I;
• END;
• RUN;
• TITLE 'THE DATA HAS ONE OBSERVATION';
• PROC PRINT; RUN;

You might also like