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}
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_];
rc = h.add();
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.
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.
@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.
@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 🙂
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.
@Quentin agree, such a masterpiece.
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
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.