Normally, it happens after importing data, but it might happen because of other reasons too: leading zeros. In general, you don’t need them, and you want to get rid of them. So, how do you remove leading zeros in SAS?
In this article, we discuss five easy methods to remove leading zeros. Each method has its use-case and its (dis)advantages. With the support of replicable examples, we show how (and when) to use each method.
Contents
How to Remove Leading Zeros
Remove All Zeros with the COMPRESS Function
The first method you can use to remove leading zeros is the COMPRESS function. However, the COMPRESS function removes all zeros from a text string. So, before you use this method, make sure that your string only has leading zeros.
In the example below, we show how to use the COMPRESS function. In this case, the COMPRESS function has two arguments:
- The text string from which you want to remove the zeros.
- A zero between quotes.
/* REMOVE ALL ZEROS */ data work.my_ds1; input my_column $; datalines; 0001 00BC 01B3 0!U% ; run; proc print data=work.my_ds1 noobs; run; data work.remove_all_zeros; set work.my_ds1; zeros_removed = compress(my_column, "0"); run; proc print data=work.remove_all_zeros noobs; run;
As the image above shows, the COMPRESS function successfully removed the zeros.
Do you know? How to Become a SAS Export with the COMPRESS Function
Remove Leading Zeros While Converting Text into Numbers
In the second method, we remove leading zeros by converting a text string into a number. Obviously, you can only use this method if and only if it is possible to read the text as a number. In other words, the text string can contain only digits.
There are two ways to convert text into a number:
- Multiply the string by one.
- Use the INPUT function to convert text into a number.
In the example below, we show how to apply both of them.
/* CONVERT TO NUMERIC */ data work.my_ds2; input my_column $; datalines; 0001 0020 0345 9009 ; run; proc print data=work.my_ds2 noobs; run; data work.convert_to_numeric; set work.my_ds2; zeros_removed_num1 = input(my_column, best12.); zeros_removed_num2 = my_column * 1; run; proc print data=work.convert_to_numeric noobs; run;
As the image above shows, you can remove leading zeros and converting text into a number in one step.
Remove Leading Zeros with VERIFY and SUBSTR
The third method to remove leading zeros in SAS combines the power of the VERIFY and SUBSTR function.
With the SUBSTR (i.e., substring) function you can extract a substring from a larger string. This function requires at least two arguments:
- A string from which you want to read a substring.
- The position you want to substring to start.
To use the SUBSTR function and remove leading zeros, you need to know the position of the first character that isn’t a zero. This position will be the start of the substring.
You can use the VERIFY function to determine the first character that isn’t a zero. In general, this function returns the position of the first character in a string that isn’t in a list of provided characters. In our case, the list of provided characters is just one character, namely a zero.
The next example shows how to combine the VERIFY and SUBSTR functions to remove leading zeros in SAS.
/* USE VERIFY + SUBSTR */ data work.my_ds3; input my_column $; datalines; 0AAA 00B2 020C 00T0 ; run; proc print data=work.my_ds3 noobs; run; data work.verify_substr; set work.my_ds3; index_first_non_zero = verify(my_column,"0"); removed_leading_zeros = substr(my_column, index_first_non_zero); run; proc print data=work.verify_substr noobs; run;
Do you know? How to Extract a Substring from a String
Remove Leading Zeros with FINDC and SUBSTR
The fourth method to remove leading zeros is very similar to the previous method. Again, we use the SUBSTR function, but this time in combination with the FINDC function.
Instead of using the VERIFY function to find the position of the first character that isn’t a zero, we will use the FINDC function. Both functions return the same result.
Below we show an example.
/* USE FINDC + SUBSTR */ data work.my_ds4; input my_column $; datalines; 0002A 00ABC 000-A 0300P ; run; proc print data=work.my_ds4 noobs; run; data work.findc_substr; set work.my_ds4; index_first_non_zero = findc(my_column,"0","K"); removed_leading_zeros = substr(my_column,index_first_non_zero); run; proc print data=work.findc_substr noobs; run;
Use Pattern Recognition to Remove Leading Zeros
The last method to remove leading zeros in SAS is with pattern recognition.
You can use pattern recognition (the PRXCHANGE function in SAS) to specify a string you want to remove from a larger string. For example, leading zeros.
With pattern recognition, you can specify that the string you want to remove should be at the start of the larger string. So, with this method, you will only remove leading zeros. All other zeros will remain untouched.
Below we show an example of how to apply pattern recognition in SAS and remove leading zeros.
/* USE PATTERN RECOGNITION */ data work.my_ds5; input my_column $; datalines; 0004AQ 000XYZ 0/00A 00006 ; run; proc print data=work.my_ds5 noobs; run; data work.pattern_recognition; set work.my_ds5; removed_leading_zeros = prxchange('s/^(0*)/ /', -1, my_column); run; proc print data=work.pattern_recognition noobs; run;
Summary
Here we show an overview of the five methods to remove leading zeros in SAS.
Method | Input Variable | Output Variable | Comments |
---|---|---|---|
COMPRESS Function | Text | Text | Removes all zeros |
INPUT Function | Text | Numeric | Converts Text into Numeric |
SUBSTR + VERIFY Function | Text | Text | Easy to understand |
SUBSTR + FINDC Function | Text | Text | Similar to SUBSTR + VERIFY |
Pattern Recognition | Text | Text | Hard to understand, but versatile |
How to Add Leading Zeros
In this article, we’ve discussed how to remove leading zeros. However, if you want to know how to add leading zeros, we recommend this article. This article (with video) shows three easy ways to add leading zeros to a string or number.
One thought on “How to Remove Leading Zeros in SAS (5 Easy Ways)”
Comments are closed.