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;
Look at your log. You get WARNINGs in the declare hash statement.
Edit: replace the equal signs with colons.
Look at your log. You get WARNINGs in the declare hash statement.
Edit: replace the equal signs with colons.
Erm, why not use the proc sort then, that is what its for and any code you write will be slower/less resource friendly?
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'.
@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.
@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
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.