The CALL SORTN and CALL SORTC routines in SAS are used to SORT the values. Most commonly these functions in SAS are used with SAS arrays.
CALL SORTN: This is used to sort the numerical
values.
Example: Suppose we have a dataset that
includes information on the cumulative spend of 8 individuals each month.
However, the data is not sorted by months. Instead, the cumulative spend for
each month is given in columns col1 to col5, but the order of the columns is
random.
Dataset:
Obs |
fname |
lname |
col1 |
col2 |
col3 |
col4 |
col5 |
1 |
Rahul |
Sharma |
$1,000.00 |
$500.00 |
$750.00 |
$1,200.00 |
$900.00 |
2 |
Amit |
Patel |
$800.00 |
$950.00 |
$1,100.00 |
$600.00 |
$850.00 |
3 |
Priya |
Gupta |
$700.00 |
$850.00 |
$1,000.00 |
$1,300.00 |
$950.00 |
4 |
Rajesh |
Singh |
$1,200.00 |
$700.00 |
$950.00 |
$800.00 |
$1,100.00 |
5 |
Meera |
Reddy |
$950.00 |
$800.00 |
$650.00 |
$1,000.00 |
$750.00 |
6 |
Sanjay |
Kumar |
$1,100.00 |
$1,200.00 |
$800.00 |
$750.00 |
$900.00 |
7 |
Anita |
Choudhar |
$600.00 |
$900.00 |
$1,200.00 |
$700.00 |
$1,000.00 |
8 |
Neha |
Joshi |
$850.00 |
$1,000.00 |
$700.00 |
$950.00 |
$1,300.00 |
Explain: To arrange these numbers in the
cumulative order of spend, we start by identifying the smallest value, which
corresponds to the spend for the first month.
For Obs 1: Rahul Sharma
·
The smallest spend value is 500, so we assign it
to Month 1.
·
Next, we move on to the second smallest spend
value, which is 750. This value becomes the cumulative spend for Month 2.
·
Continuing this process, we find that the next
highest spend value is 900. Therefore, it becomes the cumulative spend for
Month 3.
·
Moving forward, the second highest spend value of
1000 is assigned to Month 4.
· Finally, the highest spend value of 1200 is
allocated to Month 5, completing the arrangement of cumulative spend in ascending
order.
Solution: This can be easily accomplished
using the CALL SORTN routine.
data Spend_cumm;
set Spend_cumm;
call sortn(of col:);
rename col1=month1 col2=month2 col3=month3 col4=month4
col5=month5;
run;
Obs |
fname |
lname |
month1 |
month2 |
month3 |
month4 |
month5 |
1 |
Rahul |
Sharma |
$500.00 |
$750.00 |
$900.00 |
$1,000.00 |
$1,200.00 |
2 |
Amit |
Patel |
$600.00 |
$800.00 |
$850.00 |
$950.00 |
$1,100.00 |
3 |
Priya |
Gupta |
$700.00 |
$850.00 |
$950.00 |
$1,000.00 |
$1,300.00 |
4 |
Rajesh |
Singh |
$700.00 |
$800.00 |
$950.00 |
$1,100.00 |
$1,200.00 |
5 |
Meera |
Reddy |
$650.00 |
$750.00 |
$800.00 |
$950.00 |
$1,000.00 |
6 |
Sanjay |
Kumar |
$750.00 |
$800.00 |
$900.00 |
$1,100.00 |
$1,200.00 |
7 |
Anita |
Choudhar |
$600.00 |
$700.00 |
$900.00 |
$1,000.00 |
$1,200.00 |
8 |
Neha |
Joshi |
$700.00 |
$850.00 |
$950.00 |
$1,000.00 |
$1,300.00 |
CALL SORTN: This routine is similar to CALL
SORTN but instead of numerical values it is used for character values.
Question: Can we
sort column using CALL SORTN or CALL SORTN routine in SAS
Answer: No