Warren Repole

Don't Be a SAS® Dinosaur: Modernize Your SAS Programs
by Warren Repole

Variable Order: Reorder Variables Prior to Exporting


Scenario:

You want to export a SAS data set to a Microsoft Excel worksheet. The order of the columns in the exported worksheet must be different from the order of the variables in the original data set.

In SAS9, a KEEP= data set option can restrict which variables are exported, but that option does not reorder existing variables. A new data set must be created prior to the export.


The old way: Initialize Variables using the LENGTH Statement

The original approach is to declare variables in the desired order in a LENGTH statement. The LENGTH statement must appear before the SET statement to establish the variables in the proper order in the Program Data Vector (PDV).

Download this program
data neworder_length;
  length Name $ 6 Age 8 Sex $ 1 Weight Height 8;
  set sashelp.class;
run;
proc print data=neworder_length(obs=10);
  title1 "SASHELP.CLASS using LENGTH for reordering";
  title3 "[OOPS! NAME was truncated!]";
run;
proc export data=neworder_length dbms=excel replace
            outfile="c:\temp\dinosaur\reordervars.xls";
  sheet="Length";
run;

SAS Log

6945   data neworder_length;
6946     length Name $ 6 Age 8 Sex $ 1 Weight Height 8;
6947     set sashelp.class;
6948   run;
 
WARNING: Multiple lengths were specified for the variable Name by input data set(s). This
         may cause truncation of data.
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.NEWORDER_LENGTH has 19 observations and 5 variables.
 
6949   proc print data=neworder_length(obs=10);
6950     title1 "SASHELP.CLASS using LENGTH for reordering";
6951     title3 "[OOPS! NAME was truncated!]";
6952   run;
 
NOTE: There were 10 observations read from the data set WORK.NEWORDER_LENGTH.
 
6953   proc export data=neworder_length dbms=excel replace
6954               outfile="c:\temp\dinosaur\reordervars.xls";
6955     sheet="Length";
6956   run;
 
NOTE: "Length" range/sheet was successfully created.
 
 

SAS Listing Output

SASHELP.CLASS using LENGTH for reordering
 
[OOPS! NAME was truncated!]
 
Obs     Name     Age    Sex    Weight    Height
 
  1    Alfred     14     M      112.5     69.0
  2    Alice      13     F       84.0     56.5
  3    Barbar     13     F       98.0     65.3
  4    Carol      14     F      102.5     62.8
  5    Henry      14     M      102.5     63.5
  6    James      12     M       83.0     57.3
  7    Jane       12     F       84.5     59.8
  8    Janet      15     F      112.5     62.5
  9    Jeffre     13     M       84.0     62.5
 10    John       12     M       99.5     59.0
rex

The new way: Declare Variable Names using the RETAIN Statement
(available in SAS Release 82)

An alternate approach is to declare the desired order of the variable names in a RETAIN statement without referencing (directly or indirectly) any variable attributes.

Download this program
data neworder_retain;
  retain Name Age Sex Weight Height;
  set sashelp.class;
run;
proc print data=neworder_retain(obs=10);
  title1 "SASHELP.CLASS using RETAIN for reordering";
  title3 "[No side effects]";
run;
proc export data=neworder_retain dbms=excel replace
            outfile="c:\temp\dinosaur\reordervars.xls";
  sheet="Retain";
run;

SAS Log

6968   data neworder_retain;
6969     retain Name Age Sex Weight Height;
6970     set sashelp.class;
6971   run;
 
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.NEWORDER_RETAIN has 19 observations and 5 variables.
 
6972   proc print data=neworder_retain(obs=10);
6973     title1 "SASHELP.CLASS using RETAIN for reordering";
6974     title3 "[No side effects]";
6975   run;
 
NOTE: There were 10 observations read from the data set WORK.NEWORDER_RETAIN.
 
6976   proc export data=neworder_retain dbms=excel replace
6977               outfile="c:\temp\dinosaur\reordervars.xls";
6978     sheet="Retain";
6979   run;
 
NOTE: "Retain" range/sheet was successfully created.
 

SAS Listing Output

SASHELP.CLASS using RETAIN for reordering
 
[No side effects]
 
Obs    Name       Age    Sex    Weight    Height
 
  1    Alfred      14     M      112.5     69.0
  2    Alice       13     F       84.0     56.5
  3    Barbara     13     F       98.0     65.3
  4    Carol       14     F      102.5     62.8
  5    Henry       14     M      102.5     63.5
  6    James       12     M       83.0     57.3
  7    Jane        12     F       84.5     59.8
  8    Janet       15     F      112.5     62.5
  9    Jeffrey     13     M       84.0     62.5
 10    John        12     M       99.5     59.0

Advantages of the alternate approach: Good

  • No knowledge is required of the original variables' types and lengths. Only variable names are specified; all other attributes are obtained during the compilation of the SET statement.
  • The DATA step will complete successfully with no data lost. If the wrong type is specified in a LENGTH statement, the DATA step will fail during compilation. If an insufficient length is specified for a character variable, character values may be truncated.

Disadvantages of the alternate approach: Bad

  • This approach 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.]

Additional documentation for this technique can be found in SAS® 9.2 Language Reference: Dictionary. Cary, NC: SAS Institute Inc.

Visit http://support.sas.com/documentation/onlinedoc/sas9doc.html for SAS 9 documentation.

The URL for this page is http://www.repole.com/dinosaur/reordervars.html

These techniques are mentioned in other SAS references and publications:


Back to SAS Dinosaur home page

Printable copy of this page (without sample output)

SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries.

® indicates USA registration.