Desktop productivity for business analysts and programmers

SORTING NUMERIC COLUMN HEADERS

Reply
N/A
Posts: 1

SORTING NUMERIC COLUMN HEADERS

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.

Regular Contributor
Posts: 220

Re: SORTING NUMERIC COLUMN HEADERS

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!

Regular Contributor
Posts: 220

Re: SORTING NUMERIC COLUMN HEADERS

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.

Trusted Advisor
Posts: 1,052

Re: SORTING NUMERIC COLUMN HEADERS

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

Ask a Question
Discussion stats
  • 3 replies
  • 267 views
  • 0 likes
  • 3 in conversation