🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Lapis Lazuli | Level 10

## Converting numbers from rawdata to numbers and character

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## 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.

6 REPLIES 6
Super User

## 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;``````

Lapis Lazuli | Level 10

## Re: Converting numbers from rawdata to numbers and character

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

## Re: Converting numbers from rawdata to numbers and character

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.

Lapis Lazuli | Level 10

## Re: Converting numbers from rawdata to numbers and character

Is there a 'legal' way to

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

Super User

## 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.

Lapis Lazuli | Level 10

## Re: Converting numbers from rawdata to numbers and character

Thank you! I have understood that it is only one solution. I actually thought that it can be done using input/put function.
Discussion stats
• 6 replies
• 999 views
• 0 likes
• 2 in conversation