Sample 24689: Collapse a SAS data set into one observation
Illustrate how to use multidimensional
arrays by reshaping a multi-observation data set into a single observation data set.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/*********************************************************************************/
/* The DATA step DEFINE loads all variable values into a multidimensional array. */
/* */
/* The dimensions of the multidimensional array are determined using the */
/* number of observations and the number of variables that are in the data */
/* set. Thus, for this example, an array is created with dimensions of (4,3), */
/* or 4 rows and 3 columns. Note this sample only uses numeric values, so */
/* all the variables can be in the same array. */
/* */
/* In this sample, on the first iteration of the step, I=1..so the DO LOOP */
/* execution resolves to array subscripts of */
/* */
/* all(1,1)=vars(1); */
/* all(1,2)=vars(2); */
/* all(1,3)=vars(3); */
/* */
/* On the second iteration of the step I=2, so the array subscripts */
/* resolve to */
/* */
/* all(2,1)=vars(1); */
/* all(2,2)=vars(2); */
/* all(2,3)=vars(3); */
/* */
/* etc... */
/* */
/* The PUT statement is for display purposes only. The entire array is not */
/* available until after all the observations have been processed. */
/*********************************************************************************/
/* Create sample data */
data one;
input x y z;
datalines;
1 2 3
4 5 6
7 8 9
10 11 12
;
data define (drop=x y z i j);
set one end=last;
/* Create an array ALL to hold all the values in the data set. The */
/* first parameter, or dimension, is the number of rows in your data */
/* set. The second parameter or dimension, is the number of variables */
/* or columns in your data set. In this sample, 4*3=12, so we need to */
/* specify twelve new variables in the array element list, A1-A12. */
array all(4,3) a1-a12;
/* A second array, VARS, contains all the variables in the data set. */
array vars(*) x y z;
/* RETAIN the new values A1-A12 so the values are not reinitialized to */
/* missing when the DATA step iterates. */
retain a1-a12;
/* Increment I each time the DATA step iterates. I is used as the array */
/* subscript for the first dimension of ALL in the assignment statement */
/* inside the DO loop. */
i+1;
/* For every variable in the DATA set (3 in this case), assign its value */
/* into a new variable in the ALL array. */
/* */
do j=1 to 3;
all(i,j)=vars(j);
end;
put a1-a12;
/* Only output after all the reshaping is done...when we are on the last */
/* observation of ONE. */
if last then output;
run;
proc print;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
RESULTS from PUT statement
1 2 3 . . . . . . . . .
1 2 3 4 5 6 . . . . . .
1 2 3 4 5 6 7 8 9 . . .
1 2 3 4 5 6 7 8 9 10 11 12
OUTPUT from the resulting data set
Obs a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12
1 1 2 3 4 5 6 7 8 9 10 11 12
Illustrate how to use multidimensional
arrays by reshaping a multi-observation data set into a single observation data set.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step Data Management ==> Manipulation and Transformation ==> Array processing
|
| Date Modified: | 2006-12-23 03:02:56 |
| Date Created: | 2004-09-30 14:09:04 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |