5 ways to remove Leading Zeros in SAS

5 Techniques for Quickly Removing Leading Zeros in SAS

Leading zeros are a common issue in SAS data. A leading zero is a digit that appears at the beginning of a number and is used to indicate the place value of the number. For example, the number “012” has two leading zeros, indicating that the number’s actual value is 12, not 120 or 1.200.

In some cases, leading zeros may be important for maintaining the integrity of the data. For example, in a dataset of ZIP codes, the leading zero in a code like “00801” indicates that it refers to the town of Prudhoe Bay, Alaska, rather than the town of Prudhoe Bay, New Hampshire.

However, in many cases, leading zeros are simply a result of how the data was entered or exported, and they are not necessary for analysis or manipulation. In these cases, leading zeros can cause problems in SAS.

For example, suppose a SAS dataset includes a character variable with leading zeros. In that case, SAS may interpret the values as octal numbers (numbers in base 8), rather than decimal numbers (numbers in base 10). This can lead to unexpected results when the data is analyzed or manipulated.

Additionally, leading zeros can make it difficult to sort or merge datasets, as SAS may treat values with leading zeros differently than values without leading zeros.

Overall, leading zeros can be a nuisance in SAS data and can cause problems if they are not properly handled.

Let’s assume there’s a variable with leading zeros, and you want to remove the leading zeros.

This post will go through five simple ways to remove leading zeros in SAS. All approaches have different applications and disadvantages.

Method 1: Remove leading zeros using automatic variable conversion

You can remove leading zeros and convert text into a number using the automatic conversion feature of SAS.

data test;
infile datalines;
input ID $;
newID = id*1;
datalines;
 0281
 Z012
 0104
 5738
 A230
 0A23
 0024
;
5 Techniques for Quickly Removing Leading Zeros in SAS
Remove leading zeros using automatic variable conversion

Using a simple multiplication, leading zeros will be removed; however, SAS prints a Note in the log.

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
72:10

The new variable contains only numeric values. It means if you know you have only numeric values but with leading zeros, you can multiply the variable by 1 to convert it to numeric, which would automatically get rid of leading zeros,

Method 2: Remove leading zeros using the Input function

When converting a character string using the INPUT function, leading zeroes disappear. Once you have a new numeric variable, you might be satisfied with that value as a number.

data test;
 infile datalines;
 input ID $;
 newID = input(ID,??best.);
 datalines;
 0281
 Z012
 0104
 5738
 A230
 0A23
 0024
 ;
5 Techniques for Quickly Removing Leading Zeros in SAS
Remove leading zeros using the Input function

The PUT function can convert the numeric value returned by the INPUT function into a character string, allowing you to create a new character variable. 

The PUT function can convert the numeric value returned by the INPUT function into a character string, allowing you to create a new character variable. For the resultant character string to be left-justified, you must use either the COMPRESS or LEFT function to remove leading spaces.

Method 3: Remove Leading Zeros with VERIFY and SUBSTR

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.

The output of verify function is used as the 1st argument in the SUBSTR function.

data test;
 infile datalines;
 input ID $;
 v1 = verify(ID,"0");
 v2 = substr(ID,v1);
 datalines;
 0281
 Z012
 0104
 5738
 A230
 0A23
 0024
 ;
5 Techniques for Quickly Removing Leading Zeros in SAS
Remove Leading Zeros with VERIFY and SUBSTR

Method 4: Remove Leading Zeros with FINDC and SUBSTR

The FINDC function with the K argument searches for any character that does not appear in the list of characters. In this case, you would need to find the position of any other character that is not 0.

data test;
 infile datalines;
 input ID $;
 v1 = verify(ID,"0");
 v2 = substr(ID,v1);
 datalines;
 0281
 Z012
 0104
 5738
 A230
 0A23
 0024
 ;
5 Techniques for Quickly Removing Leading Zeros in SAS
Remove Leading Zeros with FINDC and SUBSTR

Method 5: Using Pattern Recognition to Remove Leading Zeros

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

 data test;
 infile datalines;
 input ID $;
newid= prxchange('s/^(0*)/ /', -1, id);
 datalines;
 0281
 Z012
 0104
 5738
 A230
 0A23
 0024
 ;
 
5 Techniques for Quickly Removing Leading Zeros in SAS
Using Pattern Recognition to Remove Leading Zeros

Using methods 3,4 and 5, the new Variable contains all the values after removing leading zeros. This trick is more robust as it takes care of character and numeric values with leading zeros.

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.