Dick Duck's SAS tips

This is the nest where I keep my SAS eggs. It's all here... Arrays, control loops, links to online SAS resources, you name it.

Tuesday, February 21, 2006

Reordering variables

You've finally got your data the way you want them, but now they're in the wrong order. Let's say we have the answers to 62 multiple-choice questions, and we want them all nicely lined up. The trick is similar to char to num conversion:

data answers (drop = i t1-t62);
set answers (rename=(q1-q62=t1-t62));
array qq1 $ t1-t62;
attrib q1-q62 length = $1;
array qq2 $ q1-q62;
do i = 1 to dim(qq1);
qq2[i] = qq1[i];
end;
run;
We need to set the length attribute if we want the new variables to have the same length as the old ones.

1 Comments:

Anonymous Anonymous said...

You can also achieve the same re-ordering of the variables with shorter code, like this:

data answers;
      attrib q1-q62 length=$1;
      set answers;
run;

Note that the attrib statement must come BEFORE the set statement for the idea to work.

10:48 pm  

Post a Comment

<< Home