How To Reverse The Order of Observations In SAS Dataset - ReadingObservations From Backward / Bottom In SAS

Hi,

As we all know that SAS processes the observation from dataset from top to bottom.

Suppose we have a situation where we want to process the bottom observation first from SAS dataset. We can not change the basic property of SAS system but we can change our SAS Dataset.

Which means, here we are changing the order of observations in SAS dataset. Our last observation would be first and the first one would get changed into last. In Simple words, just to reverse the order of observation of sas dataset.

Support we have a dataset, Named as DSN as follows :
                                                           Data DSN;
                                                           Input Name $7.;
                                                           Cards;
                                                           Alfred
                                                           Barbara
                                                           Carol
                                                           Jane
                                                           Jeffrey
                                                           Louise
                                                           Philip
                                                           Robert
                                                           Thomas
                                                           William
                                                           Run;


  Output would be as :

Here we would discuss the two ways to perform our task:

1. Just apply an index with observations and then sort the data in descending order.

                                            DATA DSN;
                                            SET DSN;
                                            INX =_N_ ;
                                            Run;

                                            PROC SORT DATA=DSN OUT=DSN_REVR (KEEP=NAME) ;
                                            BY DESCENDING INX ;
                                            RUN;



 The output would be as :


2.  We can get the desired output by using data step only as well, with the help of NOBS and POINT

                                                           DATA DSN_REVR;
                                                           Do U=NOBS TO 1 by -1;
                                                           SET DSN NOBS=NOBS POINT= U;
                                                           OUTPUT;
                                                           END;
                                                           STOP;
                                                           RUN;

2 comments:

  1. the first method with sorting is not giving the desired result

    ReplyDelete
  2. Hi Urna,

    Thanks for your reading and valuable comment.
    Can you please send me the data and code on which my first method is not working ?

    Write all the doubts in email on your data and code.

    email : sainineil@gmail.com

    ReplyDelete