DATA Step, Macro, Functions and more

ordering a variable in a custom way

Accepted Solution Solved
Reply
Super Contributor
Posts: 441
Accepted Solution

ordering a variable in a custom way

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!

 


Accepted Solutions
Solution
‎10-15-2016 02:41 PM
Super User
Posts: 5,500

Re: ordering a variable in a custom way

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


All Replies
Trusted Advisor
Posts: 1,556

Re: ordering a variable in a custom way

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;

Super Contributor
Posts: 441

Re: ordering a variable in a custom way

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!

Solution
‎10-15-2016 02:41 PM
Super User
Posts: 5,500

Re: ordering a variable in a custom way

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 258 views
  • 1 like
  • 3 in conversation