Chapter Contents |
Previous |
Next |
Example 1: Using Character Variables in an Array |
The statement inside the DO loop uses the UPCASE function to change the values of the variables in array NAMES to uppercase and then store the uppercase values in the variables in the CAPITALS array.
options nodate pageno=1 linesize=80 pagesize=60; data text; array names{*} $ n1-n10; array capitals{*} $ c1-c10; input names{*}; do i=1 to 10; capitals{i}=upcase(names{i}); end; datalines; smithers michaels gonzalez hurth frank bleigh rounder joseph peters sam ; proc print data=text; title 'Names Changed from Lowercase to Uppercase'; run;
The following output shows the TEXT data set.
Using Character Variables in an Array
Names Changed from Lowercase to Uppercase 1 Obs n1 n2 n3 n4 n5 n6 n7 n8 n9 n10 1 smithers michaels gonzalez hurth frank bleigh rounder joseph peters sam Obs c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 i 1 SMITHERS MICHAELS GONZALEZ HURTH FRANK BLEIGH ROUNDER JOSEPH PETERS SAM 11 |
Example 2: Assigning Initial Values to the Elements of an Array |
This example creates variables in the array TEST and assigns them the initial values 90, 80, and 70. It reads values into another array named SCORE and compares each element of SCORE to the corresponding element of TEST. If the value of the element in SCORE is greater than or equal to the value of the element in TEST, the variable NewScore is assigned the value in the element SCORE, and the OUTPUT statement writes the observation to the SAS data set.
The INPUT statement reads a value for the variable named ID and then reads values for all the variables in the SCORE array.
options nodate pageno=1 linesize=80 pagesize=60; data score1(drop=i); array test{3} t1-t3 (90 80 70); array score{3} s1-s3; input id score{*}; do i=1 to 3; if score{i}>=test{i} then do; NewScore=score{i}; output; end; end; datalines; 1234 99 60 82 5678 80 85 75 ; proc print noobs data=score1; title 'Data Set SCORE1'; run;
The following output shows the SCORE1 data set.
Assigning Initial Values to the Elements of an Array
Data Set SCORE1 1 New t1 t2 t3 s1 s2 s3 id Score 90 80 70 99 60 82 1234 99 90 80 70 99 60 82 1234 82 90 80 70 80 85 75 5678 85 90 80 70 80 85 75 5678 75 |
Example 3: Creating an Array for Temporary Use in the Current DATA Step |
When elements of an array are constants that are needed only for the duration of the DATA step, you can omit variables from an array group and instead use temporary array elements. You refer to temporary data elements by the array name and dimension. Although they behave like variables, temporary array elements do not have names, and they do not appear in the output data set. Temporary array elements are automatically retained, instead of being reset to missing at the beginning of the next iteration of the DATA step.
To create a temporary array, use the _TEMPORARY_ argument. The following example creates a temporary array named TEST:
options nodate pageno=1 linesize=80 pagesize=60; data score2(drop=i); array test{3} _temporary_ (90 80 70); array score{3} s1-s3; input id score{*}; do i=1 to 3; if score{i}>=test{i} then do; NewScore=score{i}; output; end; end; datalines; 1234 99 60 82 5678 80 85 75 ; proc print noobs data=score2; title 'Data Set SCORE2'; run;
The following output shows the SCORE2 data set.
Using _TEMPORARY_ Arrays
Data Set SCORE2 1 New s1 s2 s3 id Score 99 60 82 1234 99 99 60 82 1234 82 80 85 75 5678 85 80 85 75 5678 75 |
Example 4: Performing an Action on All Numeric Variables |
This example multiplies all the numeric variables in array TEST by 3.
options nodate pageno=1 linesize=80 pagesize=60; data sales; infile datalines; input Value1 Value2 Value3 Value4; datalines; 11 56 58 61 22 51 57 61 22 49 53 58 ; data convert(drop=i); set sales; array test{*} _numeric_; do i=1 to dim(test); test{i} = (test{i}*3); end; run; proc print data=convert; title 'Data Set CONVERT'; run;
The following output shows the CONVERT data set.
Output From Using a _NUMERIC_ Variable List
Data Set CONVERT 1 Obs Value1 Value2 Value3 Value4 1 33 168 174 183 2 66 153 171 183 3 66 147 159 174 |
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.