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 (May 2006, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Wed, 24 May 2006 04:35:59 -0400
Reply-To:     Jim Groeneveld <jim2stat@YAHOO.CO.UK>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Jim Groeneveld <jim2stat@YAHOO.CO.UK>
Subject:      Re: extract a string separated by '-'
Comments: To: Kitty Lee <lee.kitty@YAHOO.COM>

Dear Kitty,

You can SCAN and delimit text parts by both the '-' and the ' ' at the same time as in:

1 DATA _NULL_; 2 Value = '100101 - 100203'; 3 First = SCAN ( Value, 1 ); 4 Second = SCAN ( Value, 2 ); 5 PUT First= Second=; 6 First = SCAN ( Value, 1, ' -' ); 7 Second = SCAN ( Value, 2, ' -' ); 8 PUT First= Second=; 9 RUN;

First=100101 Second=100203 First=100101 Second=100203

Note that the delimiting characters already contain the space and the dash by default and that consecutive delimiters count as a single one!

Regards - Jim. -- Jim Groeneveld, Netherlands Statistician, SAS consultant home.hccnet.nl/jim.groeneveld

On Tue, 23 May 2006 21:02:54 -0400, Kitty Lee <lee.kitty@YAHOO.COM> wrote:

>Thanks Dan & Toby!! I just learned a new function "scan" :-) > >K. > >On Tue, 23 May 2006 17:58:51 -0700, Nordlund, Dan (DSHS) ><NordlDJ@DSHS.WA.GOV> wrote: > >>> -----Original Message----- >>> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Kitty >>> Lee >>> Sent: Tuesday, May 23, 2006 5:50 PM >>> To: SAS-L@LISTSERV.UGA.EDU >>> Subject: extract a string separated by '-' >>> >>> Hi. I have a question about extracting caseid >>> >>> My current variable, CASEID has two components separated by '-'. It looks >>> like this: >>> >>> CaseID >>> 100101 - 100203 >>> 100101 - 1002834 >>> 10020 - 100390 >>> 1003044 - 10023045 >>> >>> I wonder how can I separate out the two columns to become something like: >>> >>> CaseID_1 CaseID_2 >>> >>> 100101 100203 >>> 100101 1002834 >>> 10020 100390 >>> 1003044 10023045 >>> >>> I can't use simple substr(string, start, length) because the length of the >>> CaseIDs varies. The only clear separator is '-'. >>> >>> Thanks. :-) >>> >>> K. >> >>Kitty, >> >>You can use the SCAN function. Try something like the following: >> >>data test; >> input CaseID $1-25; >>cards; >> 100101 - 100203 >> 100101 - 1002834 >> 10020 - 100390 >> 1003044 - 10023045 >>; >>run; >>data test2; >> length CaseID_1 CaseID_2 $15; >> set test; >> CaseID_1=compress(scan(CaseID,1,'-')); >> CaseID_2=compress(scan(CaseID,2,'-')); >>run; >>proc print; >>run; >> >>Hope this is helpful, >> >>Dan >> >>Daniel J. Nordlund >>Research and Data Analysis >>Washington State Department of Social and Health Services >>Olympia, WA 98504-5204


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