DATA Step, Macro, Functions and more

HASH sort (SAScommunity.org tip-of-the-day 08OCT2013)

Reply
Respected Advisor
Posts: 3,799

HASH sort (SAScommunity.org tip-of-the-day 08OCT2013)

The sascommunity tip of the day is hash sort.

http://www.sascommunity.org/wiki/Tip_of_the_DaySmiley Surprisedctober_8

I thought it might be mildly interesting to generalize it and show an example of CALL VNEXT, my second favorite call routine.:smileysilly:

Not rigorously test your mileage may vary.

%let data=sashelp.class;
%let by=age sex;
%let keep=;
%let out=class;
data _null_;
  
if 0 then do;
     
set &data(keep=&keep);
      by &by;
      end;
  
declare hash srt(dataset:"&data",ordered:'Y',multidata:'Y');
   length _NAME_ $32;
  
do _n_ = 1 by 1;
     
call vnext(_name_);
      if missing(_name_) then leave;
      if _name_ eq '_NAME_' then leave;
      if _name_ eq: 'LAST.' then continue;
      put (_n_ _name_)(=);
      if _name_ ne: 'FIRST.' then srt.definedata(_name_);
      if _name_ eq: 'FIRST.' then srt.definekey(scan(_name_,-1,'.'));
     
end;
   srt.definedone();
   srt.output(dataset:
"&out");
  
stop;
  
run;
Super Contributor
Posts: 1,636

Re: HASH sort (SAScommunity.org tip-of-the-day 08OCT2013)

Posted in reply to data_null__

what is your first favorite call routine?

Respected Advisor
Posts: 3,799

Re: HASH sort (SAScommunity.org tip-of-the-day 08OCT2013)

CALL MISSING of course. :smileylaugh:

PROC Star
Posts: 7,474

Re: HASH sort (SAScommunity.org tip-of-the-day 08OCT2013)

Posted in reply to data_null__

DN: Do we get a step-by-step explanation so we can discover why it is your second favorite?

Super User
Super User
Posts: 7,060

Re: HASH sort (SAScommunity.org tip-of-the-day 08OCT2013)

Posted in reply to data_null__

Is there something in there that I am missing that will make sure to sort by AGE SEX instead of by SEX AGE ?

Respected Advisor
Posts: 3,799

Re: HASH sort (SAScommunity.org tip-of-the-day 08OCT2013)

The first variables are in the PDV in the same order as in the BY statement, because there was no prior reference to mess that up.  But we usually refer to LAST. before the SET and BY as in DO UNTIL(LAST....);  Not a problem here.

Ask a Question
Discussion stats
  • 5 replies
  • 349 views
  • 1 like
  • 4 in conversation