BookmarkSubscribeRSS Feed
AlexeyS
Pyrite | Level 9

Hello, I  want to convert character to numeric.

My data looks like this :

104        

104.25

104.35

103

 

I tried to use input

Price_new = input(Price, best12.2);

But the result I get as follows:

104          1.04  

104.25     104.25

104.35     104.3

103           1.03

 

As you can see the problem is when the number is without decimal point. Please help me.

Thank you

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20
data have;
input Price $;
datalines;
104
104.25
104.35
103
;

data want;
    set have;
    NumPrice=input(Price, 8.);
run;
srinath3111
Quartz | Level 8

Hi, 

 

Try

data ds;
infile datalines;
input proce_new$;
new_price=input(proce_new,12.);
datalines;
104
104.25
104.35
103
;
run;

 

Thanks,

Srinath

Kurt_Bremser
Super User

When you specify decimals in an informat (as in your input function), you force SAS to convert as many digits to decimals when no decimal point is found in the string. You only specify decimals in an informat when you need to do that conversion yourself (eg when you receive decimal fixed data from mainframes).

 

Edited because decimal point in instring overrides format definition.

Tom
Super User Tom
Super User

SAS is just doing what you asked it to do.  The decimal part of an INFORMAT tells SAS where to place the decimal point when the text string doesn't have one.  It is for reading data that has been written without the decimal point on purpose to save space in the text file.

Just use an informat of 32.. Or even better use COMMA32. as it will handle strings that include commas and dollar signs.

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
  • 822 views
  • 1 like
  • 5 in conversation