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.
A Simple SAS Example
First, let us take a look at a very simple example. A simple data step, where we create the data set class as an exact copy of the example data set 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. To do this keep in mind that the order of the variables in a SAS data set is determined by the order of which they appear 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. To do this, we use a FORMAT statement and list the varaibles 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, but this time, the columns sex, age and name appear first, because we read these variables into the PDV before the SET statement.
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.