BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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;

1 ACCEPTED SOLUTION
6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

Haikuo
Onyx | Level 15

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'.

data_null__
Jade | Level 19

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

Haikuo
Onyx | Level 15

@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

data_null__
Jade | Level 19

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;
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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