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
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.
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;
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"
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.
Thanks, finally it's working
Was not working yesterday since I combined it with another syntax which cause the error
Really appreciate your help
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
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: