Remove leading zeros with SAS

Suppose you have a variable that contains leading zeros. You wish to eliminate leading 0s from the variable in SAS.


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 $;
SAS  : Remove Leading Zeros in SAS
data abc;
set abc;
b = 1 * a;
c = prxchange('s/^0+//o',-1,a);

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


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.
Spread the Word!
Related Posts
About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he has worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and Human Resource.

8 Responses to "Remove leading zeros with SAS"
  1. thank you for this.

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

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


  4. hi sr,

    I have a real time scenario

    I have a dataset with the following info

    emp_id currency$

    1234 USD

    1289 USD

    1389 INR

    1456 INR,USD

    1567 USD,GBP,INR

    I need ouput like this

    e_id currency$

    1234 USD

    1234 USD

    1289 USD

    1389 INR

    1456 INR

    1456 USD

    1567 USD

    1567 GBP

    1567 INR

    1567 INR

    how to do it,please help me

    1. U can use scan function for doing this by creating a macro that will check for delimiter

    2. This also works...

      data dac;
      set abc;
      b= compress(a,'0');
      proc print

    3. Nope, doesn't work if the number has zeroes in other places

  5. Hi,

    Can you please explain the pattern you have written to remove leading zeroes.


Next → ← Prev

Looks like you are using an ad blocker!

To continue reading you need to turnoff adblocker and refresh the page. We rely on advertising to help fund our site. Please whitelist us if you enjoy our content.