While working on arrays in SAS, we may need to sort the array in ascending or descending order. This post will demonstrate different methods and techniques you can use to** sort an array** in SAS.

## Call SORTN Routine

The CALL SORTN Routine sorts the values of numeric arguments in an array. For example, we have created a numeric array x with ten elements. Then, to sort the array in ascending order, we have to use the Call SortN Routine with the Of keyword and specify x[*] as the argument.

The SORTN call routine will sort the values of the variables in ascending order and replace the Program Data Vector values with the new sorted values.

```
data _null_;
array x {10} (80 1 32 5 44 55 19 92 33 66);
put 'Before Sort';
put +3 (x[*])(=);
call sortn (of x[*]);
put 'After Sort';
put +3 (x[*])(=);
run;
```

**Output:**

```
Before Sort
x1=80 x2=1 x3=32 x4=5 x5=44 x6=55 x7=19 x8=92 x9=33 x10=66
After Sort
x1=1 x2=5 x3=32 x4=44 x5=80 x6=55 x7=19 x8=92 x9=33 x10=66
```

## CALL SORTC Routine

The CALL SORTC Routine sorts the values of character arguments in an array. See the example below where I have created a character array and used CALL SORTC to sort the array.

```
data _null_;
array x(8) $10
('Apricots' 'Fig' 'Blueberries' 'Gooseberries' 'Olive' 'Watermelon'
'banana' 'Jackfruit');
put 'Before Sort';
put +3 (x[*])(=);
call sortc(of x(*));
put 'After Sort';
put +3 (x[*])(=);
run;
```

## Sorting SAS Array in descending order

The Call routines SORTN and SORTC can only sort arrays in ascending order. What if you want to order it in descending order?

Here is an easy way to achieve this.

```
data _null_;
array x {10} (80 1 32 5 44 55 19 92 33 66);
put 'Before Sort';
array y[*] x10-x1;
put +3 (x[*])(=);
call sortn (of y[*]);
put 'After Sort';
put +3 (x[*])(=);
run;
```

## Bubble Sort Algorithm

The bubble sort is one of the simplest sorting algorithms. Bubble sort works through the array, comparing each value to its adjacent value and swapping them if they are not in the correct order.

The bubble sort will pass through the list until no swapping is needed and the list is in proper order.

Starting with an unsorted list of arrays [9,1,65], the bubble sort will first compare 9 to 1, determine that 9 > 1, bubble sort will swap the position resulting [1,9,6,5] and move on to the subsequent comparison. Now, the second comparison is 1 to 9. Since 1 < 9, no swapping is done.

The algorithm continues until it reaches the last element in the array, and then it starts over at the beginning (Pass 2). It will continue like this until there are no more swaps done.

```
data _null_;
array x {4} (9 1 6 5);
put 'Before Sort';
put +3 (x[*])(=);
n=dim(x);
j=1;
do until (stop);
stop=1;
put 'PASS = ' j;
do i=1 to n-1;
if x(i) > x(i+1) then
do;
put i +3 x[*] +5 'Comparing ' x(i) 'and ' x(i+1) +3 x(i) '> ' x(i+1)
'= T ' 'Swapping ' x(i) ' and ' x(i+1);
temp=x(i+1);
x(i+1)=x(i);
x(i)=temp;
stop=0;
end;
put i +3 x[*] +5 'Comparing ' x(i) 'and ' x(i+1) +3 x(i) '> ' x(i+1) '= F '
'No Swapping done';
end;
j=j+1;
end;
put 'After Sort';
put +3 (x[*])(=);
run;
```

**Output:**

```
Before Sort
x1=9 x2=1 x3=6 x4=5
PASS = 1
9 1 6 5 Comparing 9 and 1 9 > 1 = T Swapping 9 and 1
1 9 6 5 Comparing 1 and 9 1 > 9 = F No Swapping done
1 9 6 5 Comparing 9 and 6 9 > 6 = T Swapping 9 and 6
1 6 9 5 Comparing 6 and 9 6 > 9 = F No Swapping done
1 6 9 5 Comparing 9 and 5 9 > 5 = T Swapping 9 and 5
1 6 5 9 Comparing 5 and 9 5 > 9 = F No Swapping done
PASS = 2
1 6 5 9 Comparing 1 and 6 1 > 6 = F No Swapping done
1 6 5 9 Comparing 6 and 5 6 > 5 = T Swapping 6 and 5
1 5 6 9 Comparing 5 and 6 5 > 6 = F No Swapping done
1 5 6 9 Comparing 6 and 9 6 > 9 = F No Swapping done
PASS = 3
1 5 6 9 Comparing 1 and 5 1 > 5 = F No Swapping done
1 5 6 9 Comparing 5 and 6 5 > 6 = F No Swapping done
1 5 6 9 Comparing 6 and 9 6 > 9 = F No Swapping done
After Sort
x1=1 x2=5 x3=6 x4=9
```

The bubble sort works well on small data sets where processing time will be negligible. However, more efficient alternatives are available for large data sets, such as the quicksort algorithm.

The quicksort algorithm is also known as the ‘divide and conquer algorithm’. The first step is to divide the list of data values into two lists and then sort the smaller lists by splitting them up further into even smaller ones.

The algorithm isn’t as easy to implement as the bubble sort. Still, it is considered more efficient – when sorting large datasets. The quicksort algorithm is implemented in the article Quicksorting An Array by Paul Dorfman.

**We Recommend:**

SAS Essentials: A SAS Essentials Statistics Data Analytics Guide to Mastering SAS 2nd Edition – provides an introduction to SAS statistical software, the premiere statistical data analysis tool for scientific research.

SAS Essentials introduces a step-by-step approach to mastering SAS software for statistical data analysis. It’s also a valuable reference tool for any researcher currently using SAS.

Designed for those new to SAS and filled with illustrative examples, the book shows how to read, write and import data; prepare data for analysis; use SAS procedures; evaluate quantitative data; analyze counts and crosstabulation tables, and compare means using the t-test.

The book also provides instruction and examples on analysis of variance, correlation and regression, nonparametric analysis, logistic regression, creating graphs, controlling outputs using ODS, as well as advanced topics in SAS programming.

From John Wiley & Sons. Author – Alan C. Elliott