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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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