BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ilikesas
Barite | Level 11

Hi,

 

suppose that I have the followng table with the variable item:

 

item
book
table
apple

 

And I would like to order in the following way:

 

item
table 
apple
book

 

please note that the ordering is very custom-specific and I couldn't obtain it with sort by or order by options.

 

Thank you!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

There are a couple of ways to go about this.  You could create a new variable, and sort by that one.  For example:

 

select (item);

   when ('table') sortvar=1;

   when ('apple') sortvar=2;

   when ('book') sortvar=3;

end;

 

Then you can sort by SORTVAR.  

 

Technically, you asked how do this without sorting.  To do that, you still need to put the observations in order.  Here's one way (although you may need to embellish on it to get the result you are looking for):

 

data table apple book;

set have;

select (item);

   when ('table') output table;

   when ('apple') output apple;

   when ('book') output book;

end;

run;

 

data want;

set table apple book;

run;

 

Depending on the report you want in the end, some procedures support ORDER=DATA and will report the ITEMs in the same order as they appear in the data.

View solution in original post

3 REPLIES 3
Shmuel
Garnet | Level 18

Thaere are several ways to force order of variables:

 

data want;

     /*  use RETAIN or LENGTH or FORMAT statement with order desired, for example: */

     format item table apple book;

    set have;

    /* ... any cody if need */

run;

ilikesas
Barite | Level 11

Hi Shmuel,

 

I did the following code:

 

data want;
format item table apple book;
set have;
run;

 

But got the same result as before. Also, isn't the retain option for ordering variables? Becasue in my case I have one variable, its the rows within it that I want to re-order.

 

Thanks!

Astounding
PROC Star

There are a couple of ways to go about this.  You could create a new variable, and sort by that one.  For example:

 

select (item);

   when ('table') sortvar=1;

   when ('apple') sortvar=2;

   when ('book') sortvar=3;

end;

 

Then you can sort by SORTVAR.  

 

Technically, you asked how do this without sorting.  To do that, you still need to put the observations in order.  Here's one way (although you may need to embellish on it to get the result you are looking for):

 

data table apple book;

set have;

select (item);

   when ('table') output table;

   when ('apple') output apple;

   when ('book') output book;

end;

run;

 

data want;

set table apple book;

run;

 

Depending on the report you want in the end, some procedures support ORDER=DATA and will report the ITEMs in the same order as they appear in the data.

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1004 views
  • 1 like
  • 3 in conversation