DATA Step, Macro, Functions and more

Converting numbers from rawdata to numbers and character

Accepted Solution Solved
Reply
Regular Contributor
Posts: 212
Accepted Solution

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.03554859985467
2 @ 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

Posted in reply to DmytroYermak
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

View solution in original post


All Replies
Super User
Posts: 9,941

Re: Converting numbers from rawdata to numbers and character

Posted in reply to DmytroYermak

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 ]
Posted in reply to KurtBremser

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:

1.jpg

Super User
Posts: 9,941

Re: Converting numbers from rawdata to numbers and character

[ Edited ]
Posted in reply to DmytroYermak

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 ]
Posted in reply to KurtBremser

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

Posted in reply to DmytroYermak
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

Re: Converting numbers from rawdata to numbers and character

Posted in reply to KurtBremser
Thank you! I have understood that it is only one solution. I actually thought that it can be done using input/put function.
☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 129 views
  • 0 likes
  • 2 in conversation