Desktop productivity for business analysts and programmers

Convert Character to Numeric then Character again

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Convert Character to Numeric then Character again

Dear community friends,

 

I am having problem in converting a character to numeric and back to character again

The idea is to get only the numbers without the "0" in the beginning

For example, the number is 0012345678901230042 and I want to the new field to result as 12345678901230042

I tried using the following syntax:

 

LEFT(PUT(input(Account_ID,BEST19.0),19.)) end as MainAccountID

 

The result is 12345678901230040 with Character format

Seems the last number is being rounded down by the system

Really appreciate if anyone can give some help

Thanks a lot


Accepted Solutions
Solution
‎03-05-2018 10:16 PM
Super User
Super User
Posts: 9,828

Re: Convert Character to Numeric then Character again

Pleasenote, what I provided was an example to show the logic.  You would of course pass in the dataset which you have (and I don't!) using a set statement.

data want;
  set yourdata;
  account_id=substr(account_id,findc(account_id,"123456789"));
run;

Replace the word yourdata with the dataset name.

View solution in original post


All Replies
Super User
Super User
Posts: 9,828

Re: Convert Character to Numeric then Character again

SAS has a limit to the number of digits in long numbers.  I can remember off the top of my head, but I believe it is 16.  Anyways, if you just want to remove the preceding 0's:

data want;
account_id="0012345678901230042";
account_id=substr(account_id,findc(account_id,"123456789"));
run;
New Contributor
Posts: 3

Re: Convert Character to Numeric then Character again

[ Edited ]

Hi,

Thanks for the feedback

Unfortunately the solution can only affect 1 sample, while there are more than 1000 data with different numbers with the one from my example, such as

00134578919018018

00245819124782933

00011238194719972

I won't be able to specify all the numbers to only exclude the 0 from the begining

Also, I tried your solution and it still take the first "00"

 

Solution
‎03-05-2018 10:16 PM
Super User
Super User
Posts: 9,828

Re: Convert Character to Numeric then Character again

Pleasenote, what I provided was an example to show the logic.  You would of course pass in the dataset which you have (and I don't!) using a set statement.

data want;
  set yourdata;
  account_id=substr(account_id,findc(account_id,"123456789"));
run;

Replace the word yourdata with the dataset name.

New Contributor
Posts: 3

Re: Convert Character to Numeric then Character again

Thanks, finally it's working

Was not working yesterday since I combined it with another syntax which cause the error

Really appreciate your help

Super User
Posts: 10,555

Re: Convert Character to Numeric then Character again

Storing an accountID as numeric does not make sense in SAS, as the maximum reliable precision of numeric data in datasets is 15 decimal digits, so you will lose information.

Store IDs in character variables.

 

@RW9's solution will work for all your values, see

data have;
input account_id $20.;
cards;
00134578919018018
00245819124782933
00011238194719972
;
run;

data want;
set have;
account_id=substr(account_id,findc(account_id,"123456789"));
run;

proc print data=want noobs;
run;

Result:

  account_id

134578919018018
245819124782933
11238194719972 
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 10,849

Re: Convert Character to Numeric then Character again

Here are two another ways.

 

data have;
input account_id $20.;
cards;
00134578919018018
00245819124782933
00011238194719972
;
run;

data want;
set have;
way1=substr(account_id,verify(account_id,"0"));
way2=prxchange('s/^0+//',-1,strip(account_id));
run;

proc print noobs;run;
☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 247 views
  • 3 likes
  • 4 in conversation