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

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;
1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

10 REPLIES 10
PGStats
Opal | Level 21

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
saskapa
Quartz | Level 8
Hi PG,

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

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

saskapa
Quartz | Level 8
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
Ksharp
Super User

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;
saskapa
Quartz | Level 8
Thanks Ksharp. Could you please explain me '??' ? Is it just everything that is numeric only ?
ballardw
Super User

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

Ksharp
Super User

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;

saskapa
Quartz | Level 8

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

ballardw
Super User

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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