BookmarkSubscribeRSS Feed
Sagar_Pawar
Fluorite | Level 6

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
PeterClemmensen
Tourmaline | Level 20

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.

 

ChrisNZ
Tourmaline | Level 20

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.

 

PaigeMiller
Diamond | Level 26

@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
PeterClemmensen
Tourmaline | Level 20

@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 🙂

Quentin
Super User

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 Next up: Mike Raithel presenting on validating data files on Wednesday July 17. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
KachiM
Rhodochrosite | Level 12

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 828 views
  • 5 likes
  • 6 in conversation