Академический Документы
Профессиональный Документы
Культура Документы
A 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 )>>
array quiz(20);
array color(1:3) $ 1 red blue green; array pop(1:5) yr95-yr99;
array x(*) _numeric_; observation array y(*) _character_; observation array z(*) _all_;
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;
13 14 16 15 17 18 19 20
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. (How i can be dropped in the ex next) 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 ;
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
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;
`
data new; set learn.SPSS; if Height = 99 then Height = .; if Weight = 99 then Weight = .; if Age = 99 then Age = .; run; Proc print;run;
The first thing you may notice is that the program with arrays is longer than the one without arrays! However, if you had 50 or 100 variables to process, the program using arrays would not be any longer.
_CHARACTER_ includes all the character variables in the Chars data set.
Converting all character values in a SAS data set to lowercase data lower; set learn.careless; array all_chars{*} _character_; do i = 1 to dim(all_chars); all_chars{i} = lowcase(all_chars{i}); end; drop i; run;
the keyword _CHARACTER_ to reference all the character variables in data set Careless, and then use a DO loop to convert all the values to lowercase.
Temporary array
data score; array ans{10} $ 1; array key{10} $ 1 _temporary_ ('A','B','C','D','E','E','D','C','B','A'); input ID (Ans1-Ans10)($1.); RawScore = 0; do Ques = 1 to 10; RawScore + (key{Ques} eq Ans{Ques}); end; Percent = 100*RawScore/10; keep ID RawScore Percent; datalines; 123 ABCDEDDDCA 126 ABCDEEDCBA 129 DBCBCEDDEB ;
This program uses a temporary array (key) to hold the answers to the 10 quiz questions. The keyword _TEMPORARY_ tells SAS that this is a temporary array and the 10 values in parentheses are the initial values for each of the elements of this array. It is important to remember that there are no corresponding variables (Key1, Key2, and so on) in this DATA step. Also, because elements of a temporary array are retained, the 10 answer key values are available throughout the DATA step for scoring each of the student tests. The scoring is done in a DO loop. A trick is used to do the scoring: a logical comparison is performed between the student answer and the corresponding answer key. If they match, the logical comparison returns a 1 and this is added to RawScore. If not, the result is a 0 and RawScore is not incremented.
Where EXPRESSION: is any expression. The expression is evaluated at the top of the loop before the statements in the DO group are executed.
DO WHILE
DATA DOWHILE_USING; N=0; DO WHILE (N LT 5); OUTPUT; N+1; END; RUN; PROC PRINT; RUN; /* N < 5 */
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;
In the SAS log, you will see values for T{I},Y bcz of PUT statement(When you use PUT= it will show the
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;
Negative Increment DATA CHECKS; DO I= 10 TO 1 BY -1; J=I+1; OUTPUT; END; RUN; PROC PRINT; RUN;
Do and While in the same statement; DATA CHECKS; MONTH='JAN'; DO I= 10 TO 1 BY -1 WHILE(MONTH='JAN'); J=I+1; IF J=6 THEN MONTH='FEB'; OUTPUT; END; RUN; PROC PRINT; RUN;
DATA CHECKS; DO I= 2 TO 8 BY 2,11,13 TO 16; J=I+1; OUTPUT; END; RUN; PROC PRINT; RUN;
DATA CHECKS; J=0; DO YEAR=1946 TO 1949; J=J+YEAR; OUTPUT; END; RUN; PROC PRINT; RUN;
The following are the valid Do loops , you may want to use inside the program. Do I = 1 to N; Do I = N to 1 by 1; Do I=K+1 TO N-1; Do I=1 to K-1, K+1 to N; Do I=Saturday, Sunday,Monday; Do I=01jan85d ,06Apr98d;
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 ;
CHANGES MADE TO THE UPPER BOUND or INCREMENT within the DO group do not effect the no of iterations. In this example my
DATA CHECKS; K=5; DO I= 1 TO K; I=6; OUTPUT; END; RUN ; PROC PRINT; RUN;
You can write this way also; I can also use Curling braces while declaring and referencing array elements , However I cant 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;
We now know that SAS data sets created during a SAS session are automatically deleted at the end of the session, unless they are saved to a permanent data set. You can also delete SAS data sets at will during a session. This may be necessary if you have to create a large number of data sets and your computer system runs into space restrictions. PROC DATASETS is a useful SAS procedure that allows deleting SAS data sets from any library. For example, the following statements will delete DEMOG from the WORK libarary: Proc datasets library=work; Delete demog; run ; quit; You may want to delete DEMOG from WORK after you have stored it permanently in another library such as PROJ. PROC DATASETS has a lot of other
PROC DATASETS
proc datasets library=health nolist; modify group (label='Test Subjects' read=green sortedby=lname); index create vital=(birth salary) / nomiss unique; informat birth date7.; format birth date7.; label salary='current salary excluding bonus'; modify oxygen; rename oxygen=intake; label intake='Intake Measurement'; quit;
PROC DATASETS
data Dataset1; set Dataset2; run; The DATA step will accomplish the task, but it may be inefficient, especially if you are working with a large number of observations. Another method is to use a COPY statement in PROC DATASETS. LIBNAME perm 'c:\BCBS'; Data DoLoop_Test; do I = 1 to 1500; output; end; run; PROC DATASETS lib=work; copy out=perm; select DoLoop_Test ; run;