Subscribe Bookmark RSS Feed

Sort a column by variable

matt7109

Contributor

Joined:

Jun 9, 2017

Hi,

How would I go about creating a new column that contains the sorted values of one column by another variabe?

An example to hopefully clarify: if I had a table with two rows: Name and age, how would I create a third row that displays the names sorted from oldest to youngest?

 

Thanks!

3 REPLIES
Jeff_Perkinson

Community Manager

Joined:

Jun 23, 2011

What you've asked for is not easily done in a formula (that I can come up with anyway). It's relatively easy to do this interactively by using Tables->Sort to sort the table into a new table and then copy and paste the column of interest back into the original table.

 

However the question raises some interest about what you're really trying to do.

 

Generally speaking, a row of a data table represents a collection of values for a single entity – an individual student in your simple example – and each row is unrelated to other rows. I'm not sure how this new column – of Names in your example – would be useful in an analysis since the new column is only meaningful in the context of it's order.

 

Can you explain a little bit more of how you'd use this new column?

 

An example to hopefully clarify: if I had a table with two rows: Name and age, how would I create a third row that displays the names sorted from oldest to youngest?

 


PS: you seem to have confused rows and columns in your example. I'm assuming that you really meant a table with two columns and that you wanted to create a third column as your original question states. If I'm mistaken and you really meant rows, please let us know.

-Jeff
madisonquinn

Community Member

Joined:

Jun 14, 2017

Hello,

 

If the column names share the same prefix, followed by consecutive numbers, an easier way to sort them is to use a SAS variable list. In a previous example, the columns are named day001, day002, day003, …, day365. They can be represented by the syntax day001-day365. Using a SAS variable list in the RETAIN statement will effectively put the columns in sorted order.

data fix3;
  retain salesman day001-day365;
  set output;
run;

Hope it helps you, Thanks.

 

Madison Quinn
txnelson

Super User

Joined:

Jun 22, 2012

Madison,

You seem to have added a SAS/Basic response to the SAS JMP Discussion group.   

Jim