## How to remove leading zeros from character varibale and convert to numeric

Solved
Frequent Contributor
Posts: 125

# How to remove leading zeros from character varibale and convert to numeric

Hi,

I Have a character variable with comes with leading zeroes and my desired result should be numeric with removal of leading zeros

and substr the values from 3rd position  BID= Substr(AID,3,3)

AID
00000

000001

000011

000100

I20800

my desired result:
BID
0
1

11

100

800

Can anyone pls help

Accepted Solutions
Solution
‎07-26-2016 09:16 PM
PROC Star
Posts: 2,344

## Re: How to remove leading zeros from character varibale and convert to numeric

BID= input(substr(AID,3,3) ,32.);

All Replies
Super User
Posts: 23,700

## Re: How to remove leading zeros from character varibale and convert to numeric

Input ignoring the leading zeroes issue. SAS will remove them automatically.

Frequent Contributor
Posts: 125

## Re: How to remove leading zeros from character varibale and convert to numeric

But what function should i need to use to get BID from AID

Solution
‎07-26-2016 09:16 PM
PROC Star
Posts: 2,344

## Re: How to remove leading zeros from character varibale and convert to numeric

BID= input(substr(AID,3,3) ,32.);

Super User
Posts: 8,084

## Re: How to remove leading zeros from character varibale and convert to numeric

The INPUT() function can convert a character string to a new value by applying the INFORMAT that you choose.

But you will need to explain why you want to convert the last string into the number 800. Why did you ignore the letter 'I' at the beginning of the string?  Why not 20,800 instead of 800?

``````data have ;
input AID \$char12. ;
cards;
00000
000001
000011
000100
I20800
;

data want ;
set have ;
bid = input(aid,12.);
run;``````

Posts: 5,524

## Re: How to remove leading zeros from character varibale and convert to numeric

With regular expression processing:

``````length BID \$8;
BID = prxchange("s/..0*(\S+)/\1/o", 1, AID);``````
PG
Super User
Posts: 10,217

## Re: How to remove leading zeros from character varibale and convert to numeric

You should really use the {i} symbol to post data, as this uses a fixed-width font and preserves correct spacing. As it is, your example data does not match your stated requirements.

``````data want;
set have;
bid = input(substr(aid,4,3),3.);
run;``````

should do the trick.

(getting the last 3 characters from a 6-character string requires starting at position 4)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.