I am new to SAS and meet a problem to switch column content.
I have a dataset like:
data switch;
input total A1 A2 A3 A4 A5 A6 A7 A8 A9 A10;
cards;
1234567890 1 2 3 4 5 6 7 8 9 0
1234567890 1 2 3 4 5 6 7 8 9 0
1234567890 1 2 3 4 5 6 7 8 9 0
1234567890 1 2 3 4 5 6 7 8 9 0
1234567890 1 2 3 4 5 6 7 8 9 0
1234567890 1 2 3 4 5 6 7 8 9 0
1234567890 1 2 3 4 5 6 7 8 9 0
1234567890 1 2 3 4 5 6 7 8 9 0
1234567890 1 2 3 4 5 6 7 8 9 0
;
run;
So the goal is to get the values in A10 and fill into the A1, and A9 to A2, A8 to A3...etc. LIKE:
total A1 A2 A3 ...
1234567890 0 9 8
1234567890 0 9 8
1234567890 0 9 8
So generally to switch the columns(except first variable) backward.
And Array is required.
I dont know how to do it at all, because SAS reads rows(observations).
Can someone walk me through this?
Many thanks!
============================= Thanks for your thoughts everyone, I also figured out a way, which is also borrowing a temp or middle array:
data want;
set switch;
array old(1:10) A1 - A10;
array mid(1:10) B1 - B10;
do i=1 to 10;
mid[i]=old[i];
end;
array new(1:10) A1 - A10;
do i=1 to 10;
new[i]=mid[11-i]; /* input in new array with mid array in reverse order */
end;
drop B1 - B10 i; /* drop unwanted column */
run;