Tag Archive for numeric

Remove Non Numeric Characters

function  remove_non_numeric($string)
{
	return preg_replace('/D/', '', $string)
}

// OR

$result = ereg_replace( '[^0-9]+', '', $source );

source

Convert Factor Levels to Numeric in R

as.numeric(levels(f))[f]

source

Delete observations containing characters and keep numeric values only

data mydata;
input value $;
cards;
1023442
92z89
abcd
5231295
09CX42
9e122
12E3
98722
;
run;

*Suggestion #1 - simple deleting;

*Keep only numeric values;
data want1; set mydata;
if anyalpha(value)>0 then delete;
run;


*Suggestion #2 - using PRXMATCH function;
data want2; set mydata;
if prxmatch("/[a-zA-Z]/",value)=0;
/* Searches for a pattern match and returns the position at which the pattern is found */
run;


*Suggestion #3;

/*
Do you want to delete the letters?

Also, it's not a good idea to just simply delete data without knowing the value.  You may find you want to keep
those observations.  So why not try using the New Compress function in SAS9 or above and create a new variable?
*/

data want3a; set mydata;
var = Compress (value, ,"Kd") ;
*Kd is keep digits;
run;

*SAS 8;
data want3b; set mydata;
var = Compress (value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
run;

*Suggestion #4;
/* How about 12e3? It contains an alpha character but is nevertheless valid as
the external representation of a numeric value?
If so, you need to keep numbers stated in
scientific notation, then try the following method:*/

%LET N_Errors = %SYSFUNC(GETOPTION(Errors));
OPTIONS ERRORS=0;
data want4; set mydata;
if missing(input(value,best12.)) then delete;
OPTIONS ERRORS=&N_Errors;
run;

source

Check for duplicate value (numeric)

Private Sub SubjectID_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Handler

Dim SubID As String
Dim stLinkCriteria As String
Dim rsc As DAO.Recordset

Set rsc = Me.RecordsetClone

SubID = Me.SubjectID.Value
stLinkCriteria = "[SubjectID]=" & SubID

'Check table for duplicate
If DCount("SubjectID", "tblSubjects", stLinkCriteria) > 0 Then
'Undo duplicate entry
Me.Undo
'Message box warning of duplication
MsgBox "The Subject ID " & SubID & " is already in use." & _
Chr(13) & Chr(13) & "Please check to see if this patient has already been entered. " & _
"If not, then assign the patient a Subject ID.", vbInformation, "Duplicate Subject ID"
'Go to the duplicate record
'rsc.FindFirst stLinkCriteria
'Me.Bookmark = rsc.Bookmark
End If

Set rsc = Nothing

Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number = 94 Then
' Invalid use of Null error
Resume Exit_Handler
Else
End If
End Sub

source

Alpha Numeric Testing

function alpha_numeric($str)
{
return ( !preg_match("/^([-a-z0-9])+$/i", $str)) ? FALSE : TRUE;
}

source

Check if a value is numeric or not

function checkNumeric(value){
var anum=/(^d+$)|(^d+.d+$)/
if (anum.test(value))
return true;
return false;
}

source

string_pad

function pad(str, len, ch){return new Array(len-(''+str).length+1).join(ch) + str}

alert(pad(56, 4, '0')); // 0056

source