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>)
where
argument 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
|
---|
Thursday, October 29, 2015
String extraction using SAS Scan and Substr functions
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment