DATA Step, Macro, Functions and more

Perl Regular Expression simple case

Accepted Solution Solved
Reply
Contributor
Posts: 54
Accepted Solution

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

 

SasKap

 

 

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

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

run;

Accepted Solutions
Solution
‎01-11-2016 11:40 AM
Respected Advisor
Posts: 4,660

Re: Perl Regular Expression simple case

[ Edited ]

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

View solution in original post


All Replies
Solution
‎01-11-2016 11:40 AM
Respected Advisor
Posts: 4,660

Re: Perl Regular Expression simple case

[ Edited ]

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
Contributor
Posts: 54

Re: Perl Regular Expression simple case

Hi PG,

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

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.

Contributor
Posts: 54

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
Posts: 9,691

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;
Contributor
Posts: 54

Re: Perl Regular Expression simple case

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

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
Posts: 9,691

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;

Contributor
Posts: 54

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

 

Saskap

Super User
Posts: 10,550

Re: Perl Regular Expression simple case

You would find the answer under INPUT function in the documentation.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 326 views
  • 2 likes
  • 4 in conversation