Reordering Variables in SAS
My data is using Variable Range Lists so I want to ensure my input data is in the correct order. According to fellow SAS programmer Warren Repole, the old way was to initialize variables using the LENGTH statement. Example:
data neworder_length;
length Name $ 6 Age 8 Sex $ 1 Weight Height 8;
set sashelp.class;
run;
However, it's annoying to have to know the variable types and lengths. Instead, use the new way (for version 8.2 and up) by using the RETAIN statement. Example:
data neworder_retain;
retain Name Age Sex Weight Height;
set sashelp.class;
run;
All variable attributes are kept and no data is lost. You only need to know the variable names.
Warren points out this new way is "...not suitable when new variables are created during the DATA step because this technique partially relies on the automatic retain behavior for variables read through the SET statement. That retain behavior may be inappropriate for the values of newly created variables. [The FORMAT statement is a potential alternative for new variables.]"
Thanks Warren!
(Read his original post here)


Reader Comments