Fluorite | Level 6

## i want to sort the data in array and removing duplicate without using function and procs

I want to sort the element in array and removing duplicate without using any procs and function. Please help me to get this solution.

eg: {2 3 5 2 7 6 7 1}  output {1 2 3 4 5 6 7}

7 REPLIES 7
Tourmaline | Level 20

## Re: i want to sort the data in array and removing duplicate without using function and procs

You can let the hash object do the sort and dedup like this

``````data _null_;

array x {8} (2 3 5 2 7 6 7 1);

dcl hash h (ordered : "A");
h.definekey ("_x");
h.definedone();
dcl hiter hi ("h");

do _N_ = lbound(x) to hbound(x);
_x = x[_N_];
end;

put "Before Sort: " / (x[*])(=) /;

call missing(of x[*]);

do _N_ = lbound(x) to h.num_items;
rc = hi.next();
x[_N_] = _x;
end;

put "After Sort:  " / (x[*])(=);

run;``````

Result:

``` Before Sort:
x1=2 x2=3 x3=5 x4=2 x5=7 x6=6 x7=7 x8=1
After Sort:
x1=1 x2=2 x3=3 x4=5 x5=6 x6=7 x7=. x8=.```

EDIT: Just edited the code a bit.

Tourmaline | Level 20

## Re: i want to sort the data in array and removing duplicate without using function and procs

CALL SORT can sort the array for you, but you'll have to dedupe yourself.

Note that the array size doesn't change so you'll have missing values in the output if you removed duplicates.

Diamond | Level 26

## Re: i want to sort the data in array and removing duplicate without using function and procs

@Sagar_Pawar wrote:

I want to sort the element in array and removing duplicate without using any procs and function. Please help me to get this solution.

This makes no sense. Anything you do in SAS DATA steps uses a function. Even if you type < meaning less than, it is a function. Hash objects are functions.

--
Paige Miller
Tourmaline | Level 20

## Re: i want to sort the data in array and removing duplicate without using function and procs

@Sagar_Pawar , just to be clear, your have - and want arrays do not match. There is a 4 in your want array. There is not in the have array 🙂

Super User

## Re: i want to sort the data in array and removing duplicate without using function and procs

Suggest you go to lexjansen.com, search for sort array, and enjoy the results.

You can never go wrong with a paper by Paul Dofrman:

https://support.sas.com/resources/papers/proceedings/proceedings/sugi26/p096-26.pdf

And you'll find plenty more like that in Lex's index.

BASUG is hosting free webinars ! Check out our recordings of past webinars: https://www.basug.org/videos. Be sure to subscribe to our email list for notification of future BASUG events.
Tourmaline | Level 20

## Re: i want to sort the data in array and removing duplicate without using function and procs

@Quentin agree, such a masterpiece.

Rhodochrosite | Level 12

## Re: i want to sort the data in array and removing duplicate without using function and procs

Without using Functions and Procs, it can be done by programming. Assuming you have an array of medium size you can use simple and fairly easy insertion sort to sort the elements in place. The duplicate items will be marked as missing values in the array. If you have larger array of size in millions, use Quicksort but you have to modify the code to catch the duplicate items. Hope this interests you.

``````data _null_;
array k[8] _temporary_ (2 3 5 2 7 6 7 1);
do i = 1 to dim(k) - 1;
do j = i+1 to dim(k);
if k[i] = k[j] then do; k[j] =.; continue; end;
if k[i] > k[j] then do;
t = k[i];
k[i] = k[j];
k[j] = t;
end;
end;
end;

do i = 1 to dim(k);
put k[i] =;
end;

run;

k[1]=1
k[2]=.
k[3]=2
k[4]=3
k[5]=5
k[6]=6
k[7]=.
k[8]=7
``````
Discussion stats
• 7 replies
• 888 views
• 5 likes
• 6 in conversation