| In
my last blog we saw string extraction using perl function. Today we are
going to extract and concatenate character string using SAS Scan and
Substr functions. Syntax: SCAN(argument,n,<delimiters>)whereargument specifies the character variable or expression to scan. n specifies which word to read. delimiters are special characters that must be enclosed in single quotation marks (' '). If you do not specify delimiters, default delimiters are used. SUBSTR(argument,position,<n>) where argument specifies the character variable or expression to scan. position is the character position to start from. n specifies the number of characters to extract. If n is omitted, all remaining characters are included in the substring. Let us see an example:In the dataset work.Company there are four columns Name, Age, Sex and SSN.We wants to create a new data set employeeID with a column named ID which contains unique Identification number for the employees based on their last name and last four digits of their SSN. Company g
data employeeID; set company; ID = (scan(name, 1, ","))!!(substr(SSN,8,4)); run; proc print data=employeeID; run; Explanation: scan(name, 1, ",") name is the character variable to scan 1 is the position of the word to read "," is the delimiter substr(SSN,8,4) SSN is the character variable to scan. 8 is the character position to start from. 4 specifies the number of characters to extract Result
|
|---|
Saturday, September 26, 2015
String extraction using SAS Scan and Substr functions
Friday, September 25, 2015
String Extraction Using Perl Regular Expressions (PRX) in SAS
The PRXMATCH function searches source with
the perl-regular-expression and returns the position at which the string begins.
If there is no match, PRXMATCH returns a zero.
syntax : PRXMATCH (perl-regular-expression, source)
Suppose we have a list of companies and their addresses :
7‑Eleven, Inc.community relations department dallas, TX 75221-0711
20 Century Fox 10201 pico blvd, los angeles, CA 90064
APACHE prime four business park kingswells scotland, United Kingdom
We wish to extract the names of the companies. The names of the companies starts with either a upper-case letters or numbers while their addresses are in the lower-case. To do so we are going to the string extraction with the help of PRXMATCH Function.
Explanaton:
prxmatch ('/\b[a-z]\w*\b/')
In this example we are using forward slashes (/) as perl dilimiters.
\b is word boundary (a space or end-of-line)
[a-z] matches lower-case letters
\w matches any word character (upper- and lowercase letters, blank and underscore)
* matches the previous subexpression zero or more times
As can be seen in the result PRXMATCH function helped us to extract the names of the companies.
syntax : PRXMATCH (perl-regular-expression, source)
Suppose we have a list of companies and their addresses :
7‑Eleven, Inc.community relations department dallas, TX 75221-0711
20 Century Fox 10201 pico blvd, los angeles, CA 90064
APACHE prime four business park kingswells scotland, United Kingdom
We wish to extract the names of the companies. The names of the companies starts with either a upper-case letters or numbers while their addresses are in the lower-case. To do so we are going to the string extraction with the help of PRXMATCH Function.
Explanaton:
prxmatch ('/\b[a-z]\w*\b/')
In this example we are using forward slashes (/) as perl dilimiters.
\b is word boundary (a space or end-of-line)
[a-z] matches lower-case letters
\w matches any word character (upper- and lowercase letters, blank and underscore)
* matches the previous subexpression zero or more times
As can be seen in the result PRXMATCH function helped us to extract the names of the companies.
Subscribe to:
Posts (Atom)

