Published on :
Utility CREATION_INTERNE

Email Address Validation with PRXMATCH and FCMP

This code is also available in: Français
Awaiting validation
The script begins by creating a dataset named `test_email` containing a list of email addresses, some valid and some not, via `datalines`. Then, it defines a custom SAS© function `CheckMail` using `PROC FCMP`. This function takes a character string (the email address) as input, converts it to lowercase, then uses the `PRXMATCH` regular expression to check if it matches a standard email address format. The validation result (0 if invalid, 1 if valid) is returned. Finally, a second `DATA STEP` is used to apply this `CheckMail` function to each email address in the `test_email` dataset and store the result in a new variable `Cmail`.
Data Analysis

Type : CREATION_INTERNE


The data is entirely created internally within the script via `datalines` in the first `DATA STEP`. The second `DATA STEP` uses the previously created dataset.

1 Code Block
DATA STEP Data
Explanation :
This `DATA STEP` block creates a temporary dataset named `test_email`. It reads email addresses provided directly in the script via `datalines`. The `email` variable is defined as a character with a maximum length of 32.
Copied!
1DATA test_email;
2 informat email $32.;
3 INPUT email;
4 DATALINES;
5 test @test.fr
6 Test @test.fr
7 test @test..fr
8 .test @test.fr
9 .test @@test.fr
10 test @code_sas_json/marktest.json @test.fr
11 1.test @test.fr
12 n.test @test.fr
13;
14RUN;
2 Code Block
PROC FCMP
Explanation :
This `PROC FCMP` block defines a custom function named `CheckMail`. It is stored in the `work.cat_function` library under the `test` entry. The function takes an `Email` argument (character string), converts it to lowercase, then uses `PRXMATCH` with a regular expression to check the validity of the email address format. `PRXMATCH` returns the match position if found, or 0 otherwise. This result (0 or >0) is returned by the function.
Copied!
1PROC FCMP outlib=work.cat_function.test ;
2 function CheckMail(Email $);
3 mail=Email;
4 res = prxmatch("/^" !!
5 "[a-z0-9#\$%&'*+\/=\]+?^_`\{|\}~-]+" !!
6 "(?:\.[a-z0-9!#\$%&'*+\/=\?^_`\{|\}~-]+)*" !!
7 " @" !!
8 "(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+" !!
9 "[a-z0-9]" !!
10 "(?:[a-z0-9-]*[a-z0-9])?" !!
11 "/"
12 ,
13 lowcase(mail));
14 return(res);
15 endsub;
16RUN;
3 Code Block
DATA STEP
Explanation :
This second `DATA STEP` reads the `test_email` dataset created previously. For each observation, it calls the `CheckMail` function with the email address and stores the validation result (0 or 1) in a new numeric variable `Cmail`.
Copied!
1DATA test_email;
2 SET test_email;
3 Cmail=CheckMail(email);
4RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Creation date : 09/04/2017 (fr) Last update : 09/04/2017 (fr) Author(s) : Contributor(s) : Tested on SAS Studio 9.4