You're asking two questions, so I am going to answer them separately.
"Can some one suggest me a best way to sort the dataset in SAS?"
Yes, but the correct, but boring answer is that it depends largely on the specific data and circumstances. Yes, Hash Objects are sometimes faster. However a hash object is an in-memory construct. So if the size of your data exceeds the available memory allocated, then what? Then the PROC SORT may be a better choice, because it can create utility files on disk to overcome the problem. Or maybe it is a better choice to create an appropriate index and exploit it with a by statement to sort the data? However, creating the index may be both time and CPU costly. And what will you do with the index file afterwards? Delete it? And if not, how often do you want the index centiles to update, which is also CPU costly.
The short answer is: It depends. If you want a nice comparison of the different ways to sort data in SAS and what is going on under the hood, check out chapter 4 in the book High-Performance SAS Coding. It is well worth the few bucks.
Is it possible to re-write the below code using the Hash objects?
You can do something like the below, though I am not fund of the approach.
proc sql noprint;
select distinct varnm into :vars separated by ' '
from long
order by varnm;
quit;
%put &vars.;
data want(drop=i);
if 0 then set long;
attrib &vars. length=8;
array vars{*} &vars.;
if _N_ = 1 then do;
declare hash h(dataset:'long', multidata:'Y', ordered:'Y');
h.defineKey('id');
h.defineData('varnm', 'value');
h.defineDone();
end;
set short;
do i=1 by 1 while (h.do_over()=0);
vars[i]=value;
end;
run;