Control Order of Columns

When you work with data in SAS, you sometimes work with a lot of columns or variables. Usually, you have some order in your head as to how these columns should be displayed. Not only in terms of formats, but also in terms of what columns appear where. This post shows you how to control the column structure in your SAS data sets by example.

A Simple SAS Example to Control Variable Order

First, let us take a look at a very simple example. A simple data step, where we create the data set class as a copy of sashelp.class. The result in seen to the right.

data class;
   set sashelp.class;
run;
 
proc print data=class noobs;
run;

Next, let us look at how to manipulate the order of the columns. Keep in mind that the order of variables in a SAS data set is determined by their order in the PDV (Program Data Vector). Thus, if the SET statement is the first (and only) time that variables are read into the PDV, the order of the columns will be the same in the created data set as in the data set read in the SET statement.

That is why, we can easily manipulate the column order by ‘manually’ reading variables into the PDV before the SET statement. We use a FORMAT statement and list the variables we want to control in the desired order preceding the set statement. Like this

data class_2;
   format sex age name;
   set sashelp.class;
run;
 
proc print data=class_2 noobs;
run;

This creates the same data set as in the example above. However, the columns sex, age and name appear first. This happens because we read these variables into the PDV before the SET statement.

Summary

A simple FORMAT statement preceding the SET statement lets you easily control the structure of columns in your data step. If you are not familiar with why this trick works, I suggest that you take the Programming 1 Free Online Course to get the basics of data step processing down. It is time well spend.

You can download the entire code from this example here.