7
votes

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?

3

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

You certainly can change your data to be in reverse order, then process top down. Add a variable to the data set which acts as an index..then sort the data set descending by that variable.

data work.myData ;
 set work.myData ;
 indx = _n_ ;
run ;

proc sort data=work.myData ;
 by descending indx ;
run ;
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;