BookmarkSubscribeRSS Feed
data_null__
Jade | Level 19

The sascommunity tip of the day is hash sort.

http://www.sascommunity.org/wiki/Tip_of_the_Day:October_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;
5 REPLIES 5
Linlin
Lapis Lazuli | Level 10

what is your first favorite call routine?

data_null__
Jade | Level 19

CALL MISSING of course. :smileylaugh:

art297
Opal | Level 21

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

Tom
Super User Tom
Super User

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

data_null__
Jade | Level 19

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 5 replies
  • 1952 views
  • 1 like
  • 4 in conversation