Remove leading zeros with SAS

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 have a variable that contains leading zeros. You wish to eliminate leading 0s from the variable in SAS.

Example

The following program creates a sample dataset which would be used to demonstrate the trick to remove leading zeros in SAS.
data abc;
input a $;
cards;
0002255
0074
03567
000ABCD9
00ZAB74
;
run;
SAS  : Remove Leading Zeros in SAS
data abc;
set abc;
b = 1 * a;
c = prxchange('s/^0+//o',-1,a);

run;
The function PRXCHANGE() is used to perform pattern matching replacement. The pattern specified in PRXCHANGE function is a regex language,

Output

The output is shown in the image below -

Remove leading zeros in SAS
Important Points

  1. As shown above, variable b contains only numeric values. It means if you know you have only numeric values but with leading zeros, you can simply multiply the variable by 1 to convert it to numeric which would automatically get rid of leading zeros,
  2. Variable c contains all the values after removing leading zeros. This trick is more robust as it takes care of both character and numeric values with leading zeros.

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:

3 Responses to "Remove leading zeros with SAS"

  1. thank you for this.

    ReplyDelete
  2. please explain prxchange('s/^0+//o',-1,a)

    ReplyDelete
  3. Yes, could you please brief the formula used...

    ReplyDelete

Next → ← Prev