DATA Step, Macro, Functions and more

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

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 125
Accepted Solution

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: 1,760

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

Posted in reply to chennupriya

All Replies
Super User
Posts: 19,847

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

Posted in reply to chennupriya

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: 1,760

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

Posted in reply to chennupriya

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

Super User
Super User
Posts: 7,070

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

Posted in reply to chennupriya

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;

 

Respected Advisor
Posts: 4,927

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

Posted in reply to chennupriya

With regular expression processing:

 

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

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

Posted in reply to chennupriya

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.

Per your 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
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 2313 views
  • 0 likes
  • 6 in conversation