Acknowledgement: Special thanks for Dr. Kristina
Kupanoff for her input in some part of the code.
Consider this case: You collected data yielded from a repeated measures
design, in which the examinees took the same tests several times. However, the
data entry person only entered the age of the subject during the first attempt
into the database. You would like to fill in the missing cells of the field
"age" according to the value of the first instance for each subject. Some
subjects took four trials, some took five, and some took even more. It would
be easy to fix the problem by hand if there were only two subjects and nine
rows. But in reality the dataset might have more than 1000 rows. Again, this
situation necessitates automation.
The following macro function is very simple yet it accomplishes this
purpose. Let's name the preceding table as data "one" and all manipulation is
performed in dataset "two." The lag function inherits the previous value in
the field "age" and puts it into the next row and a new variable named "temp."
In the next row, if "age" is empty, then its value will be replaced with the
one in "temp," which is copied from the previous "age." Please note that the
do loop is set to run five times even though you only have four attempts.
Actually, the function still works even if you set the loop to run ten times.
If you are not sure about the maximum number of attempts, enter a large number
in the do loop.