HOW DOUBLE TRANSPOSING HAPPENS WITH DO WHITLOCK LOOP

HOW DOUBLE TRANSPOSING HAPPENS WITH DO WHITLOCK LOOP

Do Whitlock Loop

Do Whitlock loop is a powerful data step technique, more commonly called DoW loop which has a number of applications. The construction of the Do Whitlock loop is used to SET statement inside a DO UNTIL loop with BY statement to control the loop.

The general form of the DoW loop is,

Data < dataset name >;
< Stuff done before break-event >;
Do < index specs > until (break-event);
Set < dataset name >;
< Stuff done for each record >;
End;
< Stuff done after break-event >;
Run;

One of the most important applications of the DoW loop is transposing multiple variables simultaneously.

Dummy dataset tests used to explain the working of the DoW for transposing multiple variables simultaneously.

data tests;
input diseases\$ test\$ nvisit n mean;
cards;
DBHS test1 1 1000 0.1254
DBHS test1 2 1500 0.4191
DBHS test1 3 1250 0.2676
DBHS test2 1 1402 1.6314
DBHS test2 2 1420 2.2264
DBHS test2 3 1230 1.5416
DBHS test3 1 2500 185.0977
DBHS test3 2 2530 150.9920
DBHS test3 3 2140 142.1651
DBHS test4 1 1530 6.8282
DBHS test4 2 1430 7.0703
DBHS test4 3 1500 6.7980
;
run;

The following code is used to transpose ‘n’ and ‘mean’ simultaneously.

Data test_tran (Drop = nvisit n mean);
Retain diseases test n1 mean1 n2 mean2 n3 mean3;
Array n_ n1-n3;
Array mean_ mean1-mean3;
Do until (last.test);
Set tests;
By diseases test;
n_ (nvisit) =n;
mean_ (nvisit) = mean;
End;
Output;
Run;

Here we want to transpose the variables ‘n’ and ‘mean’ of the tests which is taken by each visit. Here we use numeric number of the visit to avoid unwanted warning of the log and make the calculation simple.

First we create an array, to store the transposed value of the variable which we would like to transpose and set the dataset ‘tests’ within the loop. WE also use by statement to control the loop.

The do until loop starts with least visit number 1, therefore the dimension of arrays within the loop is changed to 1. Then the first space of the array n1 is allocated to the first visit value of n_ and similarly the first space of the array mean_ is allocated to the first value of the mean and then we finally check whether the condition for true or false.

If the condition is false then take the next value and repeat this process until the condition is true. After the iteration we will get the transposed dataset ‘test_tran’ and the dimension of the array is total number of visit. The retain statement is used to arrange the order of the variables.

The output dataset form is given below, 