LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous (more recent) messageNext (less recent) messagePrevious (more recent) in topicNext (less recent) in topicPrevious (more recent) by same authorNext (less recent) by same authorPrevious page (October 2008, week 1)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Fri, 3 Oct 2008 16:17:57 -0500
Reply-To:     Mary <mlhoward@avalon.net>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Mary <mlhoward@AVALON.NET>
Subject:      Re: Puzzle: Converting 1-10 to 1,2,3,4,5,6,7,8,9,10
Comments: To: "Miller, Scott" <smiller@wvmi.org>
Content-Type: text/plain; charset="iso-8859-1"

Wow! I didn't know that the do statement could do that! Certainly one could write code to replace all dashes with a ' to '.

-Mary ----- Original Message ----- From: Miller, Scott To: Mary ; walker.627@OSU.EDU Sent: Friday, October 03, 2008 4:08 PM Subject: RE: Re: Puzzle: Converting 1-10 to 1,2,3,4,5,6,7,8,9,10

i did your approach, mary, except with a slight change in the numbers. i replaced the - with ' to ' and used a do statement to get the values. then used a similar sql statement to load those numbers into a macro var.

data z; do x=1 to 10,21,22,30 to 35,40,41; output; end; run;

Scott D. Miller, MA

Evaluation and Informatics Analyst West Virginia Medical Institute (304) 346-9864 ext 2240

Every good scientist should be one part P.T. Barnum and one part B.F. Skinner.

Sumus quid sumus

...and why did I leave the plow in the field, and look for a job in the town

-----Original Message----- From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU]On Behalf Of Mary Sent: Friday, October 03, 2008 4:48 PM To: SAS-L@LISTSERV.UGA.EDU Subject: Re: Puzzle: Converting 1-10 to 1,2,3,4,5,6,7,8,9,10

I'm not sure I can actually do this, but I'll give an approach for doing it; I think it could be done in a data step or in IML. Maybe someone else can work out the code!

1. Take the original string and form a data set that creates an observation for each value between the commas: 1-10,21,22,30-35,40,41 becomes

1-10 21 22 30-35 40 41

2. Take that new data set and search for a dash (using INDEX). If not found, then output the value. If found, then assign the start value to the part to the right of the dash, the ending value to the part to the left of the dash, using SUBSTR. Convert the start and end value to numeric, then do a do loop with an output, such as: do i=start to end; value=i; output; end;

3. Then you should wind up with values down of each of the individual values: 1 2 3 .. 41

4. Then you can get the thing into a comma delimited string by a couple of approaches. One is the Proc SQL into a variable list, such as proc sql noprint; select value into :valuelist separated by ',' from values; quit;

Or you could read the data set sequentially and then substring.

A similar approach could be used in IML- assign each value between the commas to a matrix, then process each value in the matrix by searching for the dash. If there's no dash, copy the value to the new matrix. If there is a dash, then grab the start and end value and get into a do loop and add each value as you loop to the new matrix. Then do a do loop on the entire matrix concatenating it together back into one scalar matrix.

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

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

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

CONFIDENTIALITY NOTICE: This e-mail and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed.

Any views expressed in this message are those of the individual sender, except where the sender specifies and with authority, states them to be the views of WVMI.


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