I know that Sas starts with the observation at the top of a dataset when processing and proceeds to the next until it reaches the bottom observation, but is there an easy way to make sas process the bottom observation first and then work its way to the top?
7
votes
3 Answers
15
votes
You can use nobs and point to process it backwards without having to do any intermediate steps. Here's an example:
data backwards;
do k= nobs to 1 by -1;
set sashelp.class nobs = nobs point=k;
output;
end;
stop;
run;
proc print data=sashelp.class;run;
proc print data=backwards;run;
See page 2 of this pdf for all the juicy details.
8
votes
1
votes
You can flip your observations order in a single step using PROC SQL:
proc sql;
create table work.cars as
select *
from sashelp.cars
order by monotonic() desc;
quit;
The key here is order by monotonic() desc which translates as "sort by descending observation numbers".
Alternatively, you can create a view (instead of creating a table) which will refer to the original table, but in a reverse observation number order:
proc sql;
create view work.cars_rev as
select *
from sashelp.cars
order by monotonic() desc;
quit;