6arrays &loops
6arrays &loops
q(2,4) same as q9
• 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;
• 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;
• 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;
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;