BookmarkSubscribeRSS Feed
Ratzz
Calcite | Level 5

How to sort the COLUMN headers which includes numeric,special characters and also space? I got some idea on column sorting but it applies only for proper SAS COLUMN NAMES which satisfies the rules. The above NUMERIC COLUMNS resulted after using PROC TRANSPOSE.

3 REPLIES 3
djbateman
Lapis Lazuli | Level 10

SAS seems to only put columns in the order that they are created, not numeric or alphabetical order.  If you want to specify the order of the columns, I have used one of two ways:

1.  Use PROC SQL and use the SELECT statement to list out the columns that you want in the order that you want.

2.  Use a DATA step and use the LENGTH statement to specify lengths of the columns in the order that you want (more work and less efficient).

This, however, does not seem too enjoyable if you have a lot of columns.  In that case, you could probably try something like:

proc sort data=sashelp.vcolumn (where=(libname='AAA' & memname='BBB')) out=sorted;

      by name;

run;

proc sql;

      select name into :columns separated by ', '

      from sorted

      where name not in ('','');  /* fill in the blank single quotes with the names of variables that you are not interested in sorting */

      create table sortedColumns as

      select &columns.

      from AAA.BBB;

quit;

In the above code snippet, AAA is the library containing your transposed dataset, and BBB is the name of the transposed dataset.  I just created a macro variable that stores the names of the variables that you want to sort (separated by a comma for use in PROC SQL).  Then I created a new table that keeps those columns in that sorted order.

I hope this helps!

djbateman
Lapis Lazuli | Level 10

I just noticed that you tagged this post for help in SAS Enterprise Guide.  I have never used EG, so I don't know that what I just told you will be of much help.  Hopefully the idea can somehow be incorporated, though.

TomKari
Onyx | Level 15

In EG, you can use the Query Builder to do 's first option very easily; it uses PROC SQL, and the order that you put the variables on the Select Data pane is the order they'll be in on the output dataset.

Tom

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

Explore Now →
Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 3579 views
  • 0 likes
  • 3 in conversation