- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
if substr(left(level),1,1) = '.' then level = '0' !! left(level);
The left(function) takes care of any right-alignment.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Is there a 'legal' way to
convert character '.348778715555307' to character '0.348778715555307'? To add "0" before dot.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
if substr(left(level),1,1) = '.' then level = '0' !! left(level);
The left(function) takes care of any right-alignment.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content