*---Take a list of variables (the single positional argument) that uses our std table-naming conventions and shortens the list by using variable interval notation. E.g. it converts p1i1 p1i2 p1i3 ... p1i9 to p1i1-p1i9 . The short list is stored in global variable _intlist. Must be invoked at a step boundary because it generates a data _null_ step. jgb, Jan, 2005. ---; %macro intlist(arg); %global _intlist; data _null_; retain _arg "&arg"; length _curword $32 _curtab _tab $12 _nsfx $8; retain _curtab ' ' _curno 0 _startno 0 ; _nword=1; _curword=lowcase(scan("&arg",1)); length _return $32760; retain _return; do while (_curword ne ' '); _nsfx=scan(_curword,-1,'i'); if _nsfx ne ' ' and verify(trim(_nsfx),'0123456789')=0 then do; _ltab=length(_curword) - length(_nsfx) - 1; _tab=substr(_curword,1,_ltab); *--we have a table item. it is of the form <_tab>i<_nsfx) where _tab is the table name and _nsfx the cell number. Is this part of a consecutive list?; if _curtab=' ' then do; *---starting a new table list with no active one---; _curtab=_tab; _curno=_nsfx; _startno=_curno; goto nextwrd; end; if _curtab ne _tab or ((_curno+1) ne _nsfx) then do; link add2lst; *--and now reset the current interval variables--; _curtab=_tab; _curno=_nsfx; _startno=_curno; goto nextwrd; end; *---at this point we have a table cell variable that is a continuation of the current interval--; _curno=_nsfx; end; *---processing of a numeric table cell---; else do; *---process a non-cell variable---; if _curtab ne ' ' then do; link add2lst; *--and now reset the current interval variables--; _curtab=' '; _curno=0; _startno=0; end; *--and add this non-cell to the output list--; _return=trim(_return)||' '||_curword; end; nextwrd: _nword+1; _curword=lowcase(scan("&arg",_nword)); end; *---of do while _curword, i.e. the scanning of the arg list--; *---as we leave the loop we need to see if we were processing an interval at the end, and output the last interval if necessary.---; if _curtab ne ' ' then link add2lst; call symput("_intlist",trim(_return)); put / _all_; stop; add2lst: *--we have the end of a table interval and we have to output the interval-; if _startno=_curno then _return=trimn(_return)||' '||trim(_curtab)||'i'||trim( put(_curno,4.-L)); else _return=trimn(_return)||' '||trim(_curtab)||'i'||trim( put(_startno,4.-L))||'-' ||trim(_curtab)||'i'||trim( put(_curno,4.-L)); return; run; %mend intlist; options mprint; /* test run ====================================================; %intlist (areaname p1i1 p3i1 p3i2 p3i3 h1i1 h12i3 h12i4 h12i5); %put &_intlist; ==================================end test======================== */