Restructuring a SAS Data Set: Creating Multiple Observations from a Single Observation (Multidimensional Example) - SAS Programming

In the previous example you created 3 observations from a single observation using an array.In this example,you have 6 values of X (X1 to X6) which represent X at 3 times recorded under 2 different methods.This requires a change from a simple array to a multidimensional array.Suppose, again, that you have the data set OLD with one observation per subject.This time, however, you have recorded both the time of the observation as well as the method under which the measurement was taken.

Each record contains variables X1-X6.Variables X1-X3 represent values of measurements taken for method 1,at times 1 to 3; variables X4-X6 represent values for method 2, also at times 1 to 3.You now want to create data set NEW with six observations per subject, one for each time-method combination.You also wish to create two new variables, METHOD and TIME, for each observation.

Restructuring a SAS Data Set

Here is the program that accomplishes your goal:

Data set New

Example

DATA NEW;
SET OLD?
ARRAY XX[2,3] X1-X6; 0
DO METHOD=1 TO 2; 0
DO TIME=1 TO 3; §
X=XXJMETHOD,TIME]j 0
OUTPUTj ©
END;
END;
KEEP SUBJECT METHOD TIME X;
RUN;

This program is much like the previous one, except it is in two dimensions,so it must be twice as good - certainly twice as complicated and twice as powerful. Once again, you use XX for the name of the array and X for the name of the new variable in data set NEW which will represent the measurements 0.

This time you create METHOD as a new variable as well as TIME.You do this in DO loops as before, but this time you use two nested loops, one for each dimension in the array.In nested DO loops,inner loops change faster than outer loops. In your OLD data, the measurements occur in the order: method 1-time 1, methodl-time2, methodl- time3,method2-timel, method2-time2, and method2-time3, contained in variables X1-X6.

Since time is changing from 1 through 3 within each static method, time is said to vary faster than method.This pattern can be represented in a multidimensional "2 by 3" array. Multidimensional arrays are identified by a list of array dimensions, separated by commas, with slower changing dimensions preceding faster changing ones in the list from left to right. Array XX has dimensions [2,3], and is made up of variables XI- X6.This means that XX[1,1] represents XI (method 1-time 1), XX[1,2] represents X2 (methodl-time2), and so forth, up to XX[2,3] which represents X6 (method2-time3).

The nested loops read through the data in OLD sequentially from XI through X6 and assign the proper values to variables METHOD and TIME before outputting each observation to data set NEW.First the outer DO METHOD loop sets the variable METHOD to 1 or 2 0. Next, the inner DO TIME loop cycles through the three times for each method ©.Each element of the array is therefore selected, identified as to method and time 0, and output as X to data set NEW.©

Easy? Not necessarily. Powerful? Definitely! Maybe you should LOOP through this section a few more TIMEs using different METHODs until you've got it.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAS Programming Topics