Date: Fri, 3 Oct 2008 15:59:11 -0500 Mary "SAS(r) Discussion" Mary Re: Puzzle: Converting 1-10 to 1,2,3,4,5,6,7,8,9,10 To: Toby Dunn text/plain; charset="iso-8859-1"

Very nice!

-Mary ----- Original Message ----- From: Toby Dunn To: SAS-L@LISTSERV.UGA.EDU Sent: Friday, October 03, 2008 3:55 PM Subject: Re: Puzzle: Converting 1-10 to 1,2,3,4,5,6,7,8,9,10

Data _Null_ ; Length NewString \$ 200 ; String = '1-10,21,22,30-35,40,41' ;

Do I = 1 To ( 1 + CountC( String , ',' ) ) ;

TempA = Scan( String , I , ',' ) ;

If Find( TempA , '-' ) Then Do ; Start = Input( Scan( TempA , 1 , '-' ) , Best12. ) ; Stop = Input( Scan( TempA , 2 , '-' ) , Best12. ) ;

Do K = Start To Stop ; NewString = CatX( ',' , NewString , K ) ; End ; End ; Else Do ; NewString = CatX( ',' , NewString , TempA ) ; End ;

End ;

Put NewString= ;

Run ;

On Fri, 3 Oct 2008 16:27:44 -0400, Paul Walker <walker.627@OSU.EDU> wrote:

>What is the best way to convert a string like 1-10 to a string like >1,2,3,4,5,6,7,8,9,10? The string is stored as a variable in a datastep. >I am looking for a function that could be called like this maybe. > >data _null_; > string = '1-10'; > newstring = function(string); >run; > >Add a twist to the puzzle: what if I mixed and matched. For example, a >string 1-10,21,22,30-35,40,41. > >data _null_; > string = '1-10,21,22,30-35,40,41'; > newstring = function(string); >run; > >newstring = 1,2,3,4,5,6,7,8,9,10,21,22,30,31,32,33,34,35,40,41 > >You get the idea. Any suggestions for the function? > >- Paul

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