« My Letter to Mark Shuttleworth | Main | Site Publishes Students' Art for the World to see »

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)

Posted on Thursday, February 5, 2009 by Registered CommenterJared in , | CommentsPost a Comment

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>