How do I determine whether a text string is a number/whole/integer/float?

  • Follow


5.72: How do I determine whether a scalar is a number/whole/integer/
float?


   This is more for REGEX educational purposes, since SAS have quite a
few functions that may make this kind of klingon notation more
understandable , ie anydigit, anyalpha...

    Assuming that you don't care about IEEE notations like "NaN" or
    "Infinity", you probably just want to use a regular expression.


            if (/\D/)            { print "has nondigits\n"     }
            if (/^\d+$/)         { print "is a whole number\n" }
            if (/^-?\d+$/)       { print "is an integer\n"     }
            if (/^[+-]?\d+$/)    { print "is a +/- integer\n"  }
            if (/^-?\d+\.?\d*$/) { print "is a real number\n"  }
            if (/^-?(?:\d+(?:\.\d*)?|\.\d+)$/) { print "is a decimal
number\n" }
            if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
                            { print "a C float\n" }


I am a novice with perl regex but I think ny definitions are correct

^  - start with
$  - end with
\D - any non digits
\d - digits
?  - may or may not have?
|  - or
*  - repeating

data chk;
    if prxmatch('/\D/'           ,'2E1'    ) then put  "has
nondigits"     ;
    if prxmatch('/^\d+$/'        ,'121'    ) then put  "is a whole
number(no sign)" ;
    if prxmatch('/^-?\d+$/'      ,'1234'   ) then put  "is an
integer"     ;
    if prxmatch('/^[+-]?\d+$/'   ,'+12345' ) then put  "is a +/-
integer"  ;
    if prxmatch('/^-?\d+\.?\d*$/','-0.2345') then put  "is a real
number(no plus or -.2)";
    if prxmatch('/^-?(?:\d+(?:\.\d*)?|\.\d+)$/','-22.23') then put
"is a decimal number(no plus)";
    if prxmatch('/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?
$/','-1.02E38') then put  "is a float?";
    if prxmatch('/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?
$/','+1.02E-38') then put  "is a float?";
run;
0
Reply xlr82sas (391) 3/17/2010 12:39:03 AM

I like the possibilities RegEx gives us a lot - but I believe for this
problem "normal" SAS functions are more than sufficient.

proc format;
  value _case
    1='No number or NULL'
    2='Integer'
    3='Float'
    other='Undefined'
  ;
run;

data test;
  infile datalines truncover;
  input string $ 1-20;
  format case _case.;
  testvar=input(string,?? best32.);
  select;
    when(missing(testvar))     case=1;
    when(testvar=int(testvar)) case=2;
    otherwise                  case=3;
  end;
  put case= @25 string= @40 testvar=;
datalines;
1234
12.34

1.2.34
12.a4
1 234
1 2.34
;
run;
0
Reply Patrick 3/17/2010 9:02:05 AM


On Mar 17, 2:02=A0am, Patrick <patrick.mat...@gmx.ch> wrote:
> I like the possibilities RegEx gives us a lot - but I believe for this
> problem "normal" SAS functions are more than sufficient.
>
> proc format;
> =A0 value _case
> =A0 =A0 1=3D'No number or NULL'
> =A0 =A0 2=3D'Integer'
> =A0 =A0 3=3D'Float'
> =A0 =A0 other=3D'Undefined'
> =A0 ;
> run;
>
> data test;
> =A0 infile datalines truncover;
> =A0 input string $ 1-20;
> =A0 format case _case.;
> =A0 testvar=3Dinput(string,?? best32.);
> =A0 select;
> =A0 =A0 when(missing(testvar)) =A0 =A0 case=3D1;
> =A0 =A0 when(testvar=3Dint(testvar)) case=3D2;
> =A0 =A0 otherwise =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0case=3D3;
> =A0 end;
> =A0 put case=3D @25 string=3D @40 testvar=3D;
> datalines;
> 1234
> 12.34
>
> 1.2.34
> 12.a4
> 1 234
> 1 2.34
> ;
> run;

Hi,

  Yes I agree.

  Your solution is more robust.

  Maybe my example would help users learn about regex's.

Regards
0
Reply xlr82sas 3/17/2010 6:15:34 PM

On Mar 17, 2:15=A0pm, xlr82sas <xlr82...@aol.com> wrote:
> =A0 Maybe my example would help users learn about regex's.

Check out this cartoon: http://xkcd.com/208/
0
Reply Tom 3/18/2010 2:20:36 AM

Tom
Just love it!
This link became a favourite.
Cheers, Patrick
0
Reply Patrick 3/18/2010 11:34:24 AM

4 Replies
366 Views

(page loaded in 0.132 seconds)

Similiar Articles:













7/23/2012 5:30:53 PM


Reply: