Questions:
- Why does the 3rd line of your top table have test=3, but the corresponding line in the bottom table have test=2?
- Why does the top table have 3 lines for id=5, but the bottom table have only one such line?
Assuming Q1 is just a typographical error, and the missing lines for ID=5 are just an oversight, this might be a good program to implement a simple 2-element array:
data want;
set have;
by id;
array tst {2} test1 test2;
if first.id then tst{2-(test=1)}=test;
run;
The "trick" here is the evaluation of the expression "(test=1)" which yields either a 1 (if true) or 0 (otherwise).
So, at the start of an id, TEST=1, then the result is tst{2-1}=test, i.e. tst{1}=test). The array reference tst{1} is variable test1, so it's test1=test.
But if the TEST is anything other than a one, then the result is tst{2-0}=test, i.e. tst{2}=test, meaning test2=test.