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!
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.
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;
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!
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.
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.
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.