|
hi ... another ... RIGHT+SUBSTR
use a LENGTH statement, otherwise LAST4 will have a length of 11
(the same as ORD_PROV)
and yes, the length of ORD_PROV as 11 is useful info
since it allows you to tell the SUSBTR function where to start
(at the 8th character of a right-justified value)
data x;
input ord_prov : $11.;
datalines;
22925186
21105
220952
215043
21000039825
;
run;
data x;
length last4 $4;
set x;
last4 = substr(right(ord_prov),8,4);
run;
proc print data=x noobs;
run;
OUTPUT ...
last4 ord_prov
5186 22925186
1105 21105
0952 220952
5043 215043
9825 21000039825
--
Mike Zdeb
U@Albany School of Public Health
One University Place (Room 119)
Rensselaer, New York 12144-3456
P/518-402-6479 F/630-604-1475
>
> Need to run out the door but here are two untested methods, the functions are sound the syntax may need tweeking:
>
>
>
> NewVar = SubStr( Strip( OldVar ) , Length( Strip( OldVar ) ) - 5 , Length( Strip( OldVar ) ) ) ;
>
>
>
>
> Or
>
>
>
>
>
> NewVar = PrxChange( 's/.*(/d/d/d/d)$/$2/io' , 1 , OldVar ) ;
>
>
>
>
> Toby Dunn
>
> "Don't bail. The best gold is at the bottom of barrels of crap."
> Randy Pausch
>
> "Be prepared. Luck is where preparation meets opportunity."
> Randy Pausch
>
>
>
>
>> Date: Thu, 8 Apr 2010 16:47:54 +0000
>> From: jazzlover@HOTMAIL.COM
>> Subject: Extracting Last Four Characters from Strings with Different Number of Characters
>> To: SAS-L@LISTSERV.UGA.EDU
>>
>>
>>
>>
>> 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
>>
>>
>>
>>
>>
>>
>>
>> EXAMPLE FROM WEBSITE (Doesn't solve the problem)
>>
>> SUBSTR is a character handling function that extracts a string value (a
>> subset of consecutive characters) from a �longer� string variable.
>>
>>
>>
>>
>> SUBSTR(<source name>,<first position of desired character>,<length>);
>>
>> If address='1025 Parker' then
>> str_num = SUBSTR(address,1,4); produces the result "1025"
>>
>>
>> _________________________________________________________________
>> Hotmail is redefining busy with tools for the New Busy. Get more from your inbox.
>> http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_2
>
> _________________________________________________________________
> The New Busy is not the too busy. Combine all your e-mail accounts with Hotmail.
> http://www.windowslive.com/campaign/thenewbusy?tile=multiaccount&ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_4
|