Converting numbers from rawdata to numbers and character

Solved
Regular Contributor
Posts: 212

Converting numbers from rawdata to numbers and character

[ Edited ]

Hi all,

Could you please help to convert character rawdata to numeric variable(VARN) and then into character(VARC). The view of numeric and should be identical.

Here below is the code for the rawdata.

`data test; length X 8 level \$50; infile datalines dlm="@" ; input X level;datalines;1 @ 1114.035548599854672 @ 44.10262142976916 3 @ .348778715555307 4 @ .818 5 @ 1.94897567555307E-6 ;run;`

Thank you!

*the code has been slightly modified: the number is E was added.

Accepted Solutions
Solution
‎03-19-2018 06:19 AM
Super User
Posts: 9,941

Re: Converting numbers from rawdata to numbers and character

``if substr(left(level),1,1) = '.' then level = '0' !! left(level);``

The left(function) takes care of any right-alignment.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

All Replies
Super User
Posts: 9,941

Re: Converting numbers from rawdata to numbers and character

For some of your numbers it won't be possible because they exceed the maximum precision of numbers in SAS (~15 decimal digits).

Just run a simple test:

``````data test;
length X 8 level \$50;
infile datalines dlm="@" ;
input X level;
datalines;
1 @ 1114.03554859985467
2 @ 44.10262142976916
3 @ .348778715555307
4 @ .818
;
run;

data test2;
set test;
level_num = input(level,best32.);
format level_num best32.;
level_char = put(level_num,best32.);
run;``````

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Regular Contributor
Posts: 212

Re: Converting numbers from rawdata to numbers and character

[ Edited ]

Thank you, Kurt.

I bring my apologies - I have added missed 5th line with "E" that make the task more complicated as there should not be E neither in level_num nor in level_char.

Do you mean that there is no way to leave the underlined figures in place:

Super User
Posts: 9,941

Re: Converting numbers from rawdata to numbers and character

[ Edited ]

Exactly. SAS can't store more than 15 decimal digits in datasets, as it only has 8 bytes to store real numbers.

More precision is really not needed when it comes to statistics, and in most cases where I have encountered that many digits in "numbers" they are in fact some kind of identifier and best stored in strings.

With 15 digits you can still handle the US GDP down to dimes.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Regular Contributor
Posts: 212

Re: Converting numbers from rawdata to numbers and character

[ Edited ]

Is there a 'legal' way to

convert character '.348778715555307' to character '0.348778715555307'? To add "0" before dot.

Solution
‎03-19-2018 06:19 AM
Super User
Posts: 9,941

Re: Converting numbers from rawdata to numbers and character

``if substr(left(level),1,1) = '.' then level = '0' !! left(level);``

The left(function) takes care of any right-alignment.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Regular Contributor
Posts: 212