Sample 24754: Sort variable values within an observation
Put all variable values in sorted order. This type of sort is referred to as a bubble sort.
This is Example 6.5 from Combining and Modifying SAS Data Sets - Examples.
Beginning in SAS 9.2, there are two new call routines that do something similar
to the example in the Full Code tab. They are CALL SORTC and CALL SORTN. The
two routines work in SAS 9.0 and 9.1.3, but a note is written to the log that
they are experimental. The routines can sort left to right or right to left
depending on the order in which you place them in the parentheses.
Click here to go to the documentation
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
data one;
input code1-code6;
datalines;
3 1 5 4 6 2
9 8 6 5 7 4
3 2 1 9 0 7
8 2 6 4 0 1
5 7 4 3 8 2
;
/* The DO UNTIL loop iterates until all of the variable values within an observation have */
/* been sorted. Set SORTED to 1 and SORTED will be set to 0 each time the DO group executes */
/* to reorder values. When that code does not execute, the array is already sorted, SORTED remains */
/* 1 and prevents the DO UNTIL loop from executing again. */
data varsort(keep=code1-code6);
array code(*) code1-code6;
set one;
do until (sorted);
sorted=1;
do i = 1 to dim(code)-1;
if code(i) > code(i+1) then do;
temp=code(i+1);
code(i+1)=code(i);
code(i)=temp;
sorted=0;
end;
end;
end;
run;
proc print data=varsort;
run;
/* Alternative method for SAS 9.0 and above using the SMALLEST function. */
/* The SMALLEST function returns the kth smallest non-missing value. */
/* To reorder from largest to smallest, use the LARGEST value instead. */
data two;
keep reordered:;
set one;
array code(6);
array reordered(6);
do k=1 to 6;
reordered(k)=smallest(k, of code1-code6);
end;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Obs code1 code2 code3 code4 code5 code6
1 1 2 3 4 5 6
2 4 5 6 7 8 9
3 0 1 2 3 7 9
4 0 1 2 4 6 8
5 2 3 4 5 7 8
Put all variable values in sorted order.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step Data Management ==> Manipulation and Transformation ==> Array processing SAS Reference ==> Functions ==> Descriptive Statistics ==> SMALLEST SAS Reference ==> Statements ==> Information ==> ARRAY SAS Reference ==> Functions ==> Descriptive Statistics ==> LARGEST
|
| Date Modified: | 2011-12-22 15:45:12 |
| Date Created: | 2004-09-30 14:09:10 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |