LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (July 2006, week 1)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Fri, 7 Jul 2006 15:56:15 +0000
Reply-To:     toby dunn <tobydunn@HOTMAIL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         toby dunn <tobydunn@HOTMAIL.COM>
Subject:      Macros written on a boring Friday
Content-Type: text/plain; format=flowed

Check Sort Information on a Data Set:

%Macro GetSortInfo( DataIn = , CheckSort= , TypeOfSort = , SortVars = ) ; %Local DSID RC SortType SVars Sorted ;

%Let DSID = %SysFunc( Open( &DataIn ) ) ;

%Let SortType = %SysFunc( AttrC( &DSID , SortLvl ) ) ; %Let SVars = %SysFunc( AttrC( &DSID , SortedBy) ) ;

%Let Rc = %SysFunc( Close( &DSID ) ) ;

%If ( %Length( &SortType ) > 0 ) %Then %Do ; %Let Sorted = YES ; %End ; %Else %Do ; %Let Sorted = NO ; %End ;

%If ( %Upcase( &CheckSort ) = YES ) %Then %Do ; &Sorted %End ;

%If ( %Upcase( &TypeOfSort ) = YES ) %Then %Do ; &SortType %End ;

%If ( %Upcase( &SortVars ) = YES ) %Then %Do ; &SVars %End ;

%Mend GetSortInfo ;

/* Example Usage: */

Data One ; Do I = 1 To 10 ; output ; End ; Run ;

Proc Sort Data = One out = Two ; by I ; Run ;

%put %GetSortInfo( DataIn=Two , CheckSort=Yes ) ; %put %GetSortInfo( DataIn=Two , CheckSort=Yes , TypeOfSort=Yes ) ; %put %GetSortInfo( DataIn=Two , CheckSort=Yes , TypeOfSort=Yes , SortVars=Yes) ;

Macro to sort a list of values in a Macro Variable:

%Macro OrderMList( List = ) ; %Local NElem Element Z K Type NewList OrderedList ;

%Let NElem = %Eval( %SysFunc( CountC( &List , %Str( ) ) ) + ( %Length( &List ) > 0 ));

%Do Z = 1 %To &NElem ;

%Let Element = %Scan( %Str(&List) , &Z , %Str( ) ) ;

%If ( &Type Ne Char ) %Then %Do ; %Let Type = %DataTyp( &Element ) ; %End ;

%Let MyVar&Z = &Element ;

%Let NewList = &NewList MyVar&Z ;

%End ;

%Let NewList = %SysFunc( TranWrd( %Str(&NewList) , %Str( ) , %Str(,) ) ) ;

%If ( &Type = CHAR ) %Then %Do ; %SysCall SortC( &NewList ) ; %End ; %Else %Do ; %SysCall SortN( &NewList ) ; %End ;

%Do K = 1 %to &NElem ; %Let OrderedList = &OrderedList &&&MyVar&K ; %End ;

&OrderedList ; %Mend OrdermList ;

/* Example Usage */

%Let MyList = 3 5 6 4 1 2 ; %Let MyList = %OrderMList( List = &MyList ) ;

%put &MyList ;

%Let MyList = B E C A D ; %Let MyList = %OrderMList( List = &MyList ) ;

%put &MyList ;

Toby Dunn


Back to: Top of message | Previous page | Main SAS-L page