Skip to content
9to5sas
  • Index
  • Glossary
Facebook Twitter Instagram Email RSS

  • Start Here
  • Base SAS
  • Advanced SASExpand
    • SAS Macros
    • PROC SQL
  • SAS/STATSExpand
    • SAS Analytics
    • Statistics
  • SAS Programs
9to5sas

9to5sas » ADVANCED SAS » Advanced SAS Programming » How to sort an array in SAS?

How to sort an array in SAS?

BySubhro Posted onNovember 4, 2020January 13, 2023 Last Updated:January 13, 2023
0 Comments

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

How to sort an array in SAS

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.

Page Contents

  • CALL SORT Routine
  • Call SORTN Routine
  • CALL SORTC Routine
  • Sorting SAS Array in descending order
  • Bubble Sort Algorithm

CALL SORT Routine

The CALL SORT Routine sorts the variable values.

Syntax

CALL SORT(<em>variable</em>)

Example:

The numeric variables in this example are arranged in ascending order.

data sort;
	x=2;
	y=3;
	z=1;
run;

title "Before Sorting";

proc print;
data sort2;
	set sort;
	call sort(x, y, z);
run;

title "After Sorting";

proc print;
How 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

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, if it determines that 9 > 1, the bubble sort will swap the positions resulting in [1, 9, 6, 5] and move on to the subsequent comparison. Now, the second comparison is 1 to 9. Since 1 < 9, no swapping has been 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, such as the quicksort algorithm, are available for large data sets.

The quicksort algorithm is called 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
SAS Essentials: A SAS Essentials Statistics Data Analytics Guide to Mastering SAS 2nd Edition
Buy from Amazon

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

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Check your inbox or spam folder to confirm your subscription.

Post Tags: #sas sortn function#sort array in sas#sortc function in sas#sortc in sas#sortn sas
Subhro

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.

Facebook Twitter Linkedin

Post navigation

Previous Previous
Box and Whisker Plot : Explained
NextContinue
The SAS Index Function Explained

SAS Tips in your inbox

Subscribe to 9to5sas for timely SAS tips and news delivered each month.
Learn about the latest articles, and code samples to keep your SAS skills fresh!

Your subscription is successful!

Recent Posts

  • Concatenate strings in SAS: The CAT Functions Demystified
  • 5 Techniques for Quickly Removing Leading Zeros in SAS
  • Troubleshoot Your Proc SQL Code Like a Pro with These 7 Automatic Macro Variables
  • 7 PROC SQL Options You Should Use to Debug Queries
  • How To Use The SAS Proc SQL Order By Statement?
9to5sas
  • Privacy Policy
  • Disclaimer
  • About
  • Contact
Facebook Twitter Instagram RSS Email Pinterest

Copyright © 2023 9TO5SAS, All rights reserved.

Scroll to top
  • 9to5sas Blueprint
  • About
  • About
  • Acceptable use policy
  • calculator
  • Confirm email
  • Contact
  • Contact
  • Cookie Policy
  • DISCLAIMER
  • Getting Started with SAS
  • Glossary
  • Index
  • Post #13801
  • Privacy Policy
  • Privacy policy
  • SAS Programs
  • Styles
  • Subscription confirmed
  • Terms and conditions
  • Thank You