DATA Step, Macro, Functions and more

Converting char to num. input statement yield incorrect results, unsure why?

Accepted Solution Solved
Reply
Contributor
Posts: 60
Accepted Solution

Converting char to num. input statement yield incorrect results, unsure why?

[ Edited ]

 

Here is the code

data Roc_1_01a;
set Roc_1_01; 

ROC_01=scan(_id,1," "); 

ROC_01a=ROC_01*1; 
format ROC_01a 8.2; 

ROC_00=input(ROC_01,8.2);

run;

 

 

So when I just times the character variable by 1, it is correct. I was taught not to do it that way for some reason that I can't remember.   I was told to use the input statement, which I have done. However, the input statement didn't yield the correct results.

 

this is my dataset. If you look at the 3rd row, you see the character variable -15. When I use the input statement, it converts it to -0.15 instead of -15. How do I write the code using the input statement to make it appear correct?

 

screen.png

 

 


Accepted Solutions
Solution
3 weeks ago
Super User
Posts: 13,583

Re: Converting char to num. input statement yield incorrect results, unsure why?


@Tpham wrote:

 

Here is the code

data Roc_1_01a;
set Roc_1_01; 

ROC_01=scan(_id,1," "); 

ROC_01a=ROC_01*1; 
format ROC_01a 8.2; 

ROC_00=input(ROC_01,8.2);

run;

 

 

So when I just times the character variable by 1, it is correct. I was taught not to do it that way for some reason that I can't remember.   I was told to use the input statement, which I have done. However, the input statement didn't yield the correct results.

 

this is my dataset. If you look at the 3rd row, you see the character variable -15. When I use the input statement, it converts it to -0.15 instead of -15. How do I write the code using the input statement to make it appear correct?

 

screen.png

 

 


When you use a format with a stated number of decimals you are telling sas that all of the variables without a decimal in input will be read with implied decimals.

I suggest for your use to the BEST. informat if your input has mixed numbers of decimal places.

See

data example;
   string= '1234';
   val1= input(string,best.);
   val2 = input(string,8.0);
   val3 = input(string,8.1);
   val4 = input(string,8.2);
   val5 = input(string,8.3);
   val6 = input(string,8.4);
   
run;

The input function with formats has had this behavior since the days of punch cards. It was very common to define fields on punch cards with "implied decimals" to save character space. So if columns 10 through 20 contained currency values columns 19 and 20 would have the decimal portion but the values would look like 12345678987 and the program was expected to place the decimal before the 87 using an 11.2 informat.

 

View solution in original post


All Replies
Solution
3 weeks ago
Super User
Posts: 13,583

Re: Converting char to num. input statement yield incorrect results, unsure why?


@Tpham wrote:

 

Here is the code

data Roc_1_01a;
set Roc_1_01; 

ROC_01=scan(_id,1," "); 

ROC_01a=ROC_01*1; 
format ROC_01a 8.2; 

ROC_00=input(ROC_01,8.2);

run;

 

 

So when I just times the character variable by 1, it is correct. I was taught not to do it that way for some reason that I can't remember.   I was told to use the input statement, which I have done. However, the input statement didn't yield the correct results.

 

this is my dataset. If you look at the 3rd row, you see the character variable -15. When I use the input statement, it converts it to -0.15 instead of -15. How do I write the code using the input statement to make it appear correct?

 

screen.png

 

 


When you use a format with a stated number of decimals you are telling sas that all of the variables without a decimal in input will be read with implied decimals.

I suggest for your use to the BEST. informat if your input has mixed numbers of decimal places.

See

data example;
   string= '1234';
   val1= input(string,best.);
   val2 = input(string,8.0);
   val3 = input(string,8.1);
   val4 = input(string,8.2);
   val5 = input(string,8.3);
   val6 = input(string,8.4);
   
run;

The input function with formats has had this behavior since the days of punch cards. It was very common to define fields on punch cards with "implied decimals" to save character space. So if columns 10 through 20 contained currency values columns 19 and 20 would have the decimal portion but the values would look like 12345678987 and the program was expected to place the decimal before the 87 using an 11.2 informat.

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 1 reply
  • 29 views
  • 0 likes
  • 2 in conversation