I agree that prxchange is a simpler operation to use in this case, however, when a pattern match is not satisfied it will return the originating input string. Since you are looking for a number, we also may as well create the variable with the proper data type. This help to solve the issue with prxchange returning the input string. If you want to use scan then the data needs more prep to account for all the possibilities. data _null_; input text & $50. ; * prxchange ; prx1 = input( prxchange( 's/.*\((\d{6})\).*/$1/' , -1 , text ) , ?? best. ) ; * prxmatch/prxposn ; if _n_ = 1 then pid = prxparse( '/\((\d{6})\)/o' ) ; retain pid ; if prxmatch( pid , text ) then prx2 = input( prxposn( pid , 1 , text ) , ?? best. ) ; * scan ; scan = input( scan( compress( text , '()' , 'kd' ) , 2 , '()' , 'm' ) , ?? best. ) ; if missing(scan) then scan = input( scan( cat( ' ' , compress( text , '()' , 'kd' ) , ' ' ) , 2 , '()' ) , ?? best. ) ; if ^missing(scan) then do; if scan ne mod( scan , 10**6 ) then scan = . ; end; put (_all_) (/=) ; cards; abcd(123456)gdre (123456) It was there! abcd(I'm not a number!) but I am (123456) (Eh!) No number here! 7 numbers (1234567) there... ; text=abcd(123456)gdre prx1=123456 pid=1 prx2=123456 scan=123456 text=(123456) It was there! prx1=123456 pid=1 prx2=123456 scan=123456 text=abcd(I'm not a number!) but I am (123456) prx1=123456 pid=1 prx2=123456 scan=123456 text=(Eh!) No number here! prx1=. pid=1 prx2=. scan=. text=7 numbers (1234567) there... prx1=. pid=1 prx2=. scan=.
... View more