Quartz | Level 8

## Perl Regular Expression simple case

I would like to convert variable 'res' into numeric. But I would like to do it  for the records that  only have digit value ( in our case it is  12.3 ). So I used a perl regular expression. But  I have tried write an expression so that the returned value of prxmatch is equal to 0 for 12.3 and non zero for other cases.  It doesn work...Any clue ?

Cheers

data test;
input res \$20.;
p=prxparse("/\<\>\+\D/");
pos=prxmatch(p,res);

put var= p= pos=;
cards;
12.3
<5
>9
+5
unknown
;

run;
1 ACCEPTED SOLUTION

Accepted Solutions
Opal | Level 21

## Re: Perl Regular Expression simple case

Why is +5 not a number? I would do:

data test;
if not p then p + prxparse("/^[+-]?\d+\.?\d*/");
input res \$20.;
pos=prxmatch(p,res);
put res= pos=;
cards;
12.3
<5
>9
+5
unknown
-123
;
PG
10 REPLIES 10
Opal | Level 21

## Re: Perl Regular Expression simple case

Why is +5 not a number? I would do:

data test;
if not p then p + prxparse("/^[+-]?\d+\.?\d*/");
input res \$20.;
pos=prxmatch(p,res);
put res= pos=;
cards;
12.3
<5
>9
+5
unknown
-123
;
PG
Quartz | Level 8

## Re: Perl Regular Expression simple case

Hi PG,

Thanks. Could you please explain me what does \ ^[+-]? , \.? and \d* perform ?
Super User

## Re: Perl Regular Expression simple case

What would you like for your final output for that example? I'm not sure how you are going to use the 0 result.

Quartz | Level 8

## Re: Perl Regular Expression simple case

I would like to convert res into numeric. Unfortunately there are values that cant be converted to numeric, so if I run a perl function to identify the one that can be converted into numeric ( pos =0) than I'll run an input statement on it this value. saskap
Super User

## Re: Perl Regular Expression simple case

Why not use ?? operator ?

data test;
input res \$20.;
pos=input(res,?? best32.);
* input res ?? best32. ;
put res= pos=;
cards;
12.3
<5
>9
+5
unknown
-123
;
run;
Quartz | Level 8

## Re: Perl Regular Expression simple case

Thanks Ksharp. Could you please explain me '??' ? Is it just everything that is numeric only ?
Super User

## Re: Perl Regular Expression simple case

The ?? suppresses error messages when reading non-numeric values and does not set the automatic _ERROR_ variable to 1.

Super User

## Re: Perl Regular Expression simple case

Someone has already answer your question. But I think the best way to understand it is to check SAS documnetation.

y=input(x, ?? best.);

<==>

y=input(x, ? best.);

_error_=1;

Quartz | Level 8

## Re: Perl Regular Expression simple case

Thanks. I have checked first the help documentation before raising the question but I could not find any answer. The only answer when typing '?'  in the index search from help doc , was about greplay procedure....Nothing as well with google..

Cheers