How to reorder the variables in a SAS® data set


Any of the following statements can be used to change the order of the variables in a SAS data set:

ATTRIB
ARRAY
FORMAT
INFORMAT
LENGTH
RETAIN

In order for any of these statements to have the desired effect, they must be placed before a SET, MERGE, or UPDATE statement in the DATA step. All of these statements are declarative statements which are used in building the Program Data Vector (PDV) during the compilation phase of the DATA step.

Note that only the variables whose positions are relevant need to be listed in the above statements. Variables not listed in these statements retain their same positional order following the listed variables.

The RETAIN statement is most often used to reorder variables because no other variable attribute specifications are required. The RETAIN statement has no effect on retaining values of existing variables being read from the data set.

For example, there are six variables in data set ONE in the positional order of A, B, C, D, E, and F. If the new order needs to be A, E, C, D, B, and F, then the following DATA step reorders the variables in that order:

data two;
   retain a e c d b;
   set one;
run;

As noted, the variable F is left off the RETAIN statement since the order of variable F should not change. After running the above DATA step, the variables in data set ONE are in the order of A, E, C, D, B, and F.

For more information, see What happens when the SET statement misbehaves—and how you can fix it!