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

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2023 views
  • 1 like
  • 4 in conversation