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

Hi guys, 

I'm trying to convert character values to numeric. They are costs. 

I'm using the following code: new_value = input(costs, 8.). However, original values like 1065 become 106 as well as 44.23 becomes 44.2 and original values = 0 become missing values. 

 

Can anyone help me to deal with this issue? 

 

Thank you in advance. 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

What is the LENGTH of the strings you are trying to convert?  How many leading spaces do they contain?  The examples you sight make is sound like the variable is 9 bytes long and the strings are right aligned so that reading only 8 of the 9 bytes will cause the last digit to be ignored.

 

Why did you only try to read the first 8 bytes of the strings?

Remember that the INPUT() function does not care if the WIDTH used on the INFORMAT is larger than the LENGTH of the string being read.  So just use the maximum width that the informat supports.

 

Try this instead:

new_value = input(left(costs), 32.)

 

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

What is the LENGTH of the strings you are trying to convert?  How many leading spaces do they contain?  The examples you sight make is sound like the variable is 9 bytes long and the strings are right aligned so that reading only 8 of the 9 bytes will cause the last digit to be ignored.

 

Why did you only try to read the first 8 bytes of the strings?

Remember that the INPUT() function does not care if the WIDTH used on the INFORMAT is larger than the LENGTH of the string being read.  So just use the maximum width that the informat supports.

 

Try this instead:

new_value = input(left(costs), 32.)

 

PaigeMiller
Diamond | Level 26

Hello, @NewUsrStat 

 

First of all, cost should never be character, it should always be numeric, and this is really what you need to fix. Then the problem goes away. Then you have a much superior process.

 

I cannot replicate your issue.

 

data a;
    length costs $ 8;
    costs='1065';
    output;
    costs='44.23';
    output;
run;

data b;
    set a;
    new_value = input(costs, 8.);
run;

 

Since we cannot reproduce your problem from your word description, you need to show us a portion of your actual SAS data set via working SAS data step code (Examples and instructions), and the full data step code you are using. Please do this for this question and please do this for all future questions. Thanks. 

--
Paige Miller
ballardw
Super User

The only way I can duplicate that behavior for 1065 is if the character value actually has 5 leading blanks in the value:

data example;
   x1='1065';
   x2=' 1065';
   x3='  1065';
   x4='   1065';
   x5='    1065';
   x6='     1065';
   y1= input(x1,8.);
   y2= input(x2,8.);
   y3= input(x3,8.);
   y4= input(x4,8.);
   y5= input(x5,8.);
   y6= input(x6,8.);
run;

If your character data is inconsistent then use the LEFT instruction to remove the leading spaces with the input:

data example;
   x6='     1065';
   y6= input(left(x6),8.);
run;

the 32. informat will work 1065 until you have 29 leading spaces. 29 spaces plus 4 numeric positions = 33 characters to read and 32. informat will only use the first 32.

 

This is an example of "Know thy data".

 

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 787 views
  • 1 like
  • 4 in conversation