SAS : Extracting numbers and text from alphanumeric string

Live Online Training : SAS Programming with 50+ Case Studies

- Explain Programming Concepts in Simple English
- Live Projects
- Scenario Based Questions
- Job Placement Assistance
- Get 10% off till Sept 25, 2017
- Batch starts from October 8, 2017

Suppose you wish to extract numbers and text from alphanumeric string in SAS. It is a common data manipulation task in retail and ecommerce industry. Many times numerical value in Product ID refers to a sub-product category. It's not easy to crack this puzzle as it requires a good knowledge of SAS functions. In SAS, you can use COMPRESS function to accomplish this task.

Let's create sample data

The following SAS program creates sample data which would be used further.
data abc;
input a $;
cards;
BA231
CDA224
744BAS
;
run;
data abc1;
set abc;
b = compress(a,'','A');
c = compress(a, b);

run; 
Output

Compress : Extract Numbers and Text

Variable "b" contains numeric values and variable "c" contains characters.

COMPRESS Function
It returns a character string with specified characters removed from the original string.
Syntax

COMPRESS(string, characters_to_be_removed, modifier)

  1. In the above example, compress(a, '' , 'A') uses 'A' as modifier which means removing alphabetic characters from the original string.
  2. The second compress function eliminates numeric values which are stored in variable b.

SAS Tutorials : 100 Free SAS Tutorials

About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has close to 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like retail and commercial banking, Telecom, HR and Automotive.


While I love having friends who agree, I only learn from those who don't.

Let's Get Connected: Email | LinkedIn

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

2 Responses to "SAS : Extracting numbers and text from alphanumeric string"

  1. Didnt understand comment function. plz explain

    ReplyDelete
  2. Didnt understand *compress function. plz explain

    ReplyDelete

Next → ← Prev