June 2005 Technical Tip SAS: Parsing email addresses

A student recently asked me if SAS could be used to parse email addresses into their various components. Well, as my good friend Rob says, "The answer to any question which begins Can SAS do... is always Yes!" I should mention that Rob is a SAS employee. Nevertheless, if you want to manipulate irregular data, SAS is your friend!

In the example which follows we will assume the emails in the file are properly formatted. The contents of the input file c:\emails.txt is as follows:

bill.qualls@deltaco.com
emmakay90@aol.com
bessie@azhighways.com
pskepnek@chicagobears.com
hannah@nhsmi.org
Download file here.

The fully commented source code is as follows:

filename myinput "c:\emails.txt";
filename myoutput "c:\parsed.txt";

data _null_;                    /* not creating a SAS dataset */
infile myinput;                           /* input a flatfile */
file myoutput;                           /* output a flatfile */

length email $40;
input email $;                        /* input has email only */

atsign = index(email,"@");             /* position of at sign */
name = substr(email,1,atsign-1);     /* name preceded at sign */
remains = substr(email,atsign+1);              /* what's left */
period = index(remains,".");          /* period after at sign */
domain = substr(remains,1,period-1);        /* isolate domain */
type = substr(remains,period+1);       /* type follows period */

put @1 name $20. @21 domain $20. @41 type $3.;    /* write it */

run;
Download source code here.

The output file c:\parsed.txt is as follows:

bill.qualls         deltaco             com
emmakay90           aol                 com
bessie              azhighways          com
pskepnek            chicagobears        com
hannah              nhsmi               org
Download file here.

Want to know more about SAS? Give us a call! Mainframe or PC, you can count on Caliber Data Training for top quality education.


Go to the articles index. Written by Bill Qualls. Copyright © 2005 by Caliber Data Training 800.938.1222