BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jdub
Calcite | Level 5

Hi

I am new to using perl regular expressions.  I have a variable, call it area, whose observations correspond to a 32 character string.  Within each observation there is a six digit numeric string contained in parenthesis.  I want to create a new variable, call it space, whose observations correspond to the the six digit numeric string from the original variable titled area.  Below is my first pass at coding.  With the sql approach, I am getting an error that says ' Function PRXPARSE requires at most 1 argument(s)'.   Could someone help me out to correctly code the creation of the variable titled space?

proc sql;

create table test as

select a.*, prxparse("/\(\d\d\d\d\d\d\)/" a.area) as space

from data1 a;

quit;

I also tried:

data test;

if _n_=1 then perl=prxparse("/\(\d\d\d\d\d\d\)\");

retain perl;

set data1;

space=prxmatch(perl,area);

run;

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

This will match a number of any number of digits between parentheses within your string. If you want exclusively numbers of 6 digits, replace the + in the pattern with {6}

data test;

area = "abcd(123456)gdre";

prx = prxparse("/\((\d+)\)/o");

if prxmatch(prx, area) then space = prxposn(prx,1,area);

put (_all_) (=);

run;

PG

PG

View solution in original post

5 REPLIES 5
Reeza
Super User

Can you post sample data and sample results.

Personally I'd use the scan function instead.

Scan(space, 2, "()");

PGStats
Opal | Level 21

More efficient than regular expressions indeed. All depends on how regular your data is :

data _null_;

length space area $50;

input area&;

space = Scan(area, 2, "()");

put (_all_) (+2 =);

datalines;

abcd(123456)gdre

(123456) It was there!

abcd(I'm not a number!) but I am (123456)

(Eh!) No number here!

;

  space=123456   area=abcd(123456)gdre

  space=It was there!   area=(123456) It was there!

  space=I'm not a number!   area=abcd(I'm not a number!) but I am (123456)

  space=No number here!   area=(Eh!) No number here!

PG

PG
PGStats
Opal | Level 21

This will match a number of any number of digits between parentheses within your string. If you want exclusively numbers of 6 digits, replace the + in the pattern with {6}

data test;

area = "abcd(123456)gdre";

prx = prxparse("/\((\d+)\)/o");

if prxmatch(prx, area) then space = prxposn(prx,1,area);

put (_all_) (=);

run;

PG

PG
Tom
Super User Tom
Super User

PRXCHANGE() makes more sense to me for this operation.

You can probably get a better regex from someone else, but this seems to work.

data test;

  area='there are 123456 counties';

  space=prxchange("s/.*(\d\d\d\d\d\d).*/$1/",-1,area);

  put (_all_) (=);

run;

FriedEgg
SAS Employee

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=.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 804 views
  • 1 like
  • 5 in conversation