BookmarkSubscribeRSS Feed
daisy6
Quartz | Level 8

Hello,

I have strings need to convert to numeric.

 

data test;

input value $12.;

datalines;

0.4567

2.678E-2

3.5679E-3

-0.1987

0.03489

0.5634

run;

how to get the numeric values. Thanks!

 

4 REPLIES 4
Reeza
Super User

If you remove the $12. SAS reads in the values as numerics accurately. 

 

If you need to convert it, use INPUT with a best format, but it's more efficient to just delete the $12 in my opinion.

 

data test;
input value $12.;
value_num =input(value, best12.);
datalines;
0.4567
2.678E-2
3.5679E-3
-0.1987
0.03489
0.5634
;
run;
ballardw
Super User

If you have a data set and want to add a numeric variable:

data work.want;
   set test;
   value_num = input(value,best.);
run;

If you want to keep the same name for the variable you need a bit more as you cannot change the type of a variable once it is set.

 

data work.want;
   set test (rename= (value=oldvalue) );
   value = input(oldvalue,best.);
   drop oldvalue;
run;
SuryaKiran
Meteorite | Level 14

Hi,

 

Give the appropriate informat in the input statement.

 

data test;
format value 12.4;
input value e12.5;
datalines;
0.4567
2.678E-2
3.5679E-3
-0.1987
0.03489
0.5634
;
run;

Thanks,

Suryakiran

Thanks,
Suryakiran
Tom
Super User Tom
Super User

You can use the INPUT() function to convert a string to a number. The normal numeric informat will work with those strings.

num_value = input(value,12.);

 

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
  • 4 replies
  • 2876 views
  • 1 like
  • 5 in conversation