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 (April 2010, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Thu, 8 Apr 2010 15:44:00 -0400
Reply-To:     Sigurd Hermansen <HERMANS1@WESTAT.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Sigurd Hermansen <HERMANS1@WESTAT.COM>
Subject:      Re: Extracting Last Four Characters from Strings with Different
              Number of Characters
Comments: To: M Jazzlover <jazzlover@HOTMAIL.COM>
In-Reply-To:  <COL102-W19BE444ABF64F4795156EAA6160@phx.gbl>
Content-Type: text/plain; charset="us-ascii"

~Malcolm: I'd opt for the solution with the fewest moving parts. Here's a SAS V9.2 Data step program that compares different solutions for a slightly more complex set of test data:

data x; input ord_prov : $11.; output; ord_prov=' '||ord_prov||' '; output; datalines; 22925186 21105 220952 215043 21000039825

21 ; run;

data x; length last4M $4; set x; last4M = substr(right(ord_prov),8,4); OldVar=ord_prov; NewVarT1 = SubStr( Strip( OldVar ) , Length( Strip( OldVar ) ) - 5 , Length( Strip( OldVar ) ) ) ; NewVarT2 = PrxChange( 's/.*(/d/d/d/d)$/$2/io' , 1 , OldVar ) ; NEWVARD = substr(ORD_PROV,length(ORD_PROV)-3,4); NewVarS = reverse(substr(reverse(strip(OldVar)),1,4)); put last4M= NewVarT1= NewVarT2= NEWVARD= NewVarS= ; run;

Any of the solutions works well for a typical set of values. Most require adjustments for some relatively minor variations in values. S

-----Original Message----- From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of M Jazzlover Sent: Thursday, April 08, 2010 2:04 PM To: SAS-L@LISTSERV.UGA.EDU Subject: Re: Extracting Last Four Characters from Strings with Different Number of Characters

SAS-Lers,

Thank you very much to those who responded so promptly to my question.

I am now able to compute the new variable correctly!

Best wishes,

~Malcolm

> > -----Original Message----- > > From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of M > > Jazzlover > > Sent: Thursday, April 08, 2010 9:48 AM > > To: SAS-L@LISTSERV.UGA.EDU > > Subject: Extracting Last Four Characters from Strings with Different Number of > > Characters >> > Hello SAS-L Group. >> > > > I found the instructions on extracting a subset of characters from a longer character > > string (see below, "EXAMPLE FROM WEBSITE." However, this solution doesn't > > seem to help when the original string values do not have the same numbers of > > characters. > > > > MY QUESTION: > > How to select a subset of the rightmost 4 characters from a longer character string > > in which the number of characters differs. > > > > > > OUR ORIGINAL VARIABLE > > > > Variable name = ORD_PROV > > > > Length (from Proc contents, = 11; not sure if this is relevant) > > >> > > Sample Values of ORD_PROV Values I want for NEWVAR (the rightmost 4 > > places) > > > > 1. 22925186 1. 5186 > > 2. 21105 2. 1105 > > 3. 220952 3. 0952 > > 4. 215043 4. 5043 > > 5. 21000039825 5. 9825 > > > > > I'd appreciate your help with the language for this (non-macro, if possible). It's > > necessary for us > > > > so that we can do a merge using the 4-character NEWVAR. > > > > Thank you! > > > > > > ~Malcolm >

_________________________________________________________________ The New Busy think 9 to 5 is a cute idea. Combine multiple calendars with Hotmail. http://www.windowslive.com/campaign/thenewbusy?tile=multicalendar&ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_5


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