-
Notifications
You must be signed in to change notification settings - Fork 22
/
order.sas
37 lines (32 loc) · 1.14 KB
/
order.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*http://listserv.uga.edu/cgi-bin/wa?A2=ind0410b&L=sas-l&F=P&S=&P=16933 Roland
%let listx= bbb nnnn eer rr rt x x v zz aaaaaa gggg j;
%put %order(&listx) ;
*/
%macro order(str);
%local swap words i list newlist scana scanb len lena lenb endpos;
%let list=%sysfunc(compbl(%sysfunc(trim(%sysfunc(left(&str))))));
%let words=%length(&list)-%length(%sysfunc(compress(&list,%str())))+1;
%let len=%length(&list);
%let swap=1;
%do %while(&swap);
%let swap=0;
%do i=1 %to %eval(&words-1);
%let scana=%scan(&list,&i,%str( ));
%let scanb=%scan(&list,%eval(&i+1),%str( ));
%let lena=%length(&scana);
%let lenb=%length(&scanb);
%if "&scana" GT "&scanb" %then %do;
%let swap=1;
%let newlist=;
%let pos=%index(&list,&scana &scanb);
%if &pos gt 1 %then %let newlist=%substr(&list,1,%eval(&pos-1));
%let newlist=%left(&newlist &scanb &scana);
%let endpos=%eval(&pos+&lena+&lenb);
%if &endpos LT &len
%then %let newlist=%sysfunc(left(&newlist%substr(&list,%eval(&endpos+1))));
%let list=&newlist;
%end;
%end;
%end;
&list
%mend;