DATA Step, Macro, Functions and more

Hash sort

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

Hash sort

I’d like to sort a dataset tab1 by ch, but I do not get the expected result:

 

data tab1;

input ch:$30. val1:8.;

datalines;

D 9

B 1

A 6

C 7

run;

 

data tab1_sort;

if 0 then set tab1;

declare hash sortha(dataset: 'tab1', ordered='d', multidata='y');

declare hiter iter('sortha');

rc=sortha.definekey ("ch" );

rc=sortha.defineData("ch", "val1");

rc=sortha.definedone();

do while ( rc = 0 );

   rc = iter.prev(); if rc=0 then output tab1_sort;

end;

run;

 

I’d like to obtain the same result from the following proc sort:

 

proc sort data=tab1 out=tab1_sort;

by ch;

run;


Accepted Solutions
Solution
‎04-08-2016 01:55 PM
Super User
Posts: 7,764

Re: Hash sort

[ Edited ]
Posted in reply to mario_pellegrini

Look at your log. You get WARNINGs in the declare hash statement.

 

Edit: replace the equal signs with colons.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎04-08-2016 01:55 PM
Super User
Posts: 7,764

Re: Hash sort

[ Edited ]
Posted in reply to mario_pellegrini

Look at your log. You get WARNINGs in the declare hash statement.

 

Edit: replace the equal signs with colons.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,942

Re: Hash sort

Posted in reply to mario_pellegrini

Erm, why not use the proc sort then, that is what its for and any code you write will be slower/less resource friendly?

Respected Advisor
Posts: 3,156

Re: Hash sort

Posted in reply to mario_pellegrini

Also, in your Hash code, you specified ordered='d'; however, I couldn't find the equivalent settings in your Proc Sort, as the default is 'ascend'.

Respected Advisor
Posts: 3,799

Re: Hash sort

@Haikuo Using a HASH to sort it appears that ordering is applied to all keys and cannot duplicate PROC SORT exactly where any KEY may have a different descending/ascending option.  Do you know if each key variable's order can be specified using HASH.

Respected Advisor
Posts: 3,156

Re: Hash sort

Posted in reply to data_null__

@data_null__, I am not aware of such an implemenation of Hash object in SAS 9.4. So you are correct, the option will apply to ALL keys.

 

Haikuo

Respected Advisor
Posts: 3,799

Re: Hash sort

[ Edited ]
Posted in reply to mario_pellegrini

Would OUTPUT method be easier.

 

data tab1;
   input ch:$30. val1:8.;
   datalines;
D 9
D 8
B 1
A 6
C 7
;;;; run; data _null_; if 0 then set tab1; declare hash sortha(dataset: 'tab1', ordered:'d', multidata:'y'); sortha.definekey ("ch" ); sortha.defineData(all:'yes'); sortha.definedone(); sortha.output(dataset:'tab1_sort'); stop; run; proc sort data=tab1 out=tab2_sort; by descending ch; run; proc compare base=tab1_sort compre=tab2_sort listequalvars; run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 428 views
  • 2 likes
  • 5 in conversation