Wednesday, January 4, 2023
HomeData Science5 Methods To Take away Main Zeros In SAS?

5 Methods To Take away Main Zeros In SAS?


Let’s assume there’s a variable with main zeros, and also you need to take away the main zeros.

This put up will undergo 5 easy methods to take away main zeros in SAS. All approaches have totally different functions and downsides.

Technique 1: Take away main zeros utilizing computerized variable conversion

You’ll be able to take away main zeros and convert textual content right into a quantity utilizing the automated conversion characteristic of SAS.

knowledge check;
infile datalines;
enter ID $;
newID = id*1;
datalines;
 0281
 Z012
 0104
 5738
 A230
 0A23
 0024
;
5 ways to remove Leading Zeros in SAS?
Take away main zeros utilizing computerized variable conversion

Utilizing a easy multiplication, main zeros will probably be eliminated; nevertheless, SAS prints a Observe within the log.

NOTE: Character values have been transformed to numeric values on the locations given by: (Line):(Column).
72:10

The brand new variable comprises solely numeric values. It means if you realize you will have solely numeric values however with main zeros, you’ll be able to multiply the variable by 1 to transform it to numeric, which might mechanically eliminate main zeros,

Technique 2: Take away main zeros utilizing the Enter perform

When changing a personality string utilizing the INPUT perform, main zeroes disappear. Upon getting a brand new numeric variable, you is likely to be happy with that worth as a quantity.

knowledge check;
 infile datalines;
 enter ID $;
 newID = enter(ID,??greatest.);
 datalines;
 0281
 Z012
 0104
 5738
 A230
 0A23
 0024
 ;
5 ways to remove Leading Zeros in SAS?
Take away main zeros utilizing the Enter perform

The PUT perform can convert the numeric worth returned by the INPUT perform into a character string, permitting you to create a new character variable. 

The PUT perform can convert the numeric worth returned by the INPUT perform into a personality string, permitting you to create a brand new character variable. For the resultant character string to be left-justified, you need to use both the COMPRESS or LEFT perform to take away main areas.

Technique 3: Take away Main Zeros with VERIFY and SUBSTR

You need to use the VERIFY perform to find out the primary character that isn’t a zero. On the whole, this perform returns the place of the primary character in a string that isn’t in a listing of supplied characters.

The output of confirm perform is used as the first 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 ways to remove 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 ways to remove 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 ways to remove 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.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments