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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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