This tutorial explains how to reverse order of data in SAS. Reversing the order of data means arranging the data in the opposite order from its original sequence.
Reverse order of data |
Create Sample Data
Let's create sample data for demonstration purpose.
data temp;
input var1 @@;
cards;
2 9 6 4 3 8
;
run;
Method I : With Sorting
In the SAS program below, we created a new dataset "temp2" by copying the contents of the original dataset "temp". It adds a new variable "i" to "temp2" containing the observation numbers. Then it sorts "temp2" in descending order based on the "i" variable.
data temp2; set temp; i = _N_; run; proc sort data = temp2; by descending i; run;Method II : Without Sorting
data temp2; do i = nobs to 1 by -1; set temp nobs = nobs point=i; output; end; stop; run;
- The POINT= option allows us to point to and select a given observation.
- The NOBS=creates and names a temporary variable whose value is usually the total number of observations in the input data set or data sets. If more than one data set is listed in the SET statement, NOBS= the total number of observations in the data sets that are listed.
Nice
ReplyDeleteHi sir could you please explain
DeleteDifference between join and merge.
Why did we use stop?
ReplyDeleteBecause Point uses Direct Access Read Mode while reading input data set and hence it does not understand EOF marker which specifies end of file. If you do not specify STOP statement then the above program will go in infinite loop. To avoid that we use stop statement to explicitly specify end of input file.
DeleteHope this helps.
Can u explain more examples on this.
ReplyDeletetq