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