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

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.

1 ACCEPTED SOLUTION
6 REPLIES 6
Kurt_Bremser
Super User

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;

 

DmytroYermak
Lapis Lazuli | Level 10

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

Kurt_Bremser
Super User

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.

DmytroYermak
Lapis Lazuli | Level 10

Is there a 'legal' way to 

 

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

 

DmytroYermak
Lapis Lazuli | Level 10
Thank you! I have understood that it is only one solution. I actually thought that it can be done using input/put function.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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