Wednesday, July 9, 2014

Check if Numeric or not

/* MACRO THE CHECK THE STRING, IS IT A NUMERIC OR NOT */

/********** ISNUM MACRO STARTS **********/
%MACRO ISNUM(STR);
    verify(trim(left(&str)),'0123456789')=0 or /*number only*/
    verify(trim(left(&str)),'0123456789.')=0
    and not indexc(substr(&str,indexc(&str,'.')+1), '.') or /*allow only one '.'*/
    verify(trim(left(&str)),'0123456789.+-')=0
    and not indexc(substr(&str,indexc(&str,'.')+1), '.')
    and (indexc(&str,'+-')=1
    and not indexc(substr(&str,2),'+-') /*allow only one leading '+' or '-'*/
    and indexc(&str,'0123456789.') > 1 ) or /* '+-' must followed by number*/
    compress(&str)='' /*'', ' ', or multiple ' ' is numeric*/
%MEND;
/********** ISNUM MACRO ENDS **********/

DATA T ;
    VAR = 'TESTING' ;
    VAR2 = '32465746' ;
    IF %ISNUM(VAR) THEN OUTPUT ;
RUN ;