DATA Step, Macro, Functions and more

Question about automatic variable type conversions character to numeric

Accepted Solution Solved
Reply
Occasional Contributor yo1
Occasional Contributor
Posts: 19
Accepted Solution

Question about automatic variable type conversions character to numeric

Hello,

 

I am trying to find documentation on why one should not mulitply one to a character variable to convert character to numeric (unless its really needed).  I found some information on the paper 248-2012 on page 9 about it; however, I know that there is more information warning others not to perform this method other than saying its bad practice or sloppy coding. 

 

Any resources about not using this method is greatly apprecated.

 

Best


Accepted Solutions
Solution
‎04-18-2017 10:51 AM
Super User
Posts: 19,789

Re: Question about automatic variable type conversions character to numeric

There is no other reason other than bad practice and sloppy coding.

 

Usually these types of things will mask errors that you won't notice or it will be hard to find actual issues when you have them if you get used to seeing these NOTES and ignoring them. So not coding in this manner will help you in the long run. 

 


yo1 wrote:

Hello,

 

I am trying to find documentation on why one should not mulitply one to a character variable to convert character to numeric (unless its really needed).  I found some information on the paper 248-2012 on page 9 about it; however, I know that there is more information warning others not to perform this method other than saying its bad practice or sloppy coding. 

 

Any resources about not using this method is greatly apprecated.

 

Best


 

 

View solution in original post


All Replies
Super User
Posts: 11,343

Re: Question about automatic variable type conversions character to numeric

Personally my opinion is it indicates a lack of understanding of the data involved. If a variable should be numeric then it should be numeric all the time.

 

Also in some shops there are rules about data standards and messages about type conversion occuring in the log indicate that the standards are not followed.

 

And in general coding terrms how much difference is there really between

numval = val*1;

and

numval = input(val, best.);

Occasional Contributor yo1
Occasional Contributor
Posts: 19

Re: Question about automatic variable type conversions character to numeric

Hello Ballardw,

 

Thank you for your response. I agree with you in that some shops have rules about data standards and that should be the case especially when a specific data products are looked at carefully by many.   However, I disagree with the idea that when a variable is numeric it should stay numeric. I have come across situations where a specific key variable (which is numeric) used in merging tables needs to be converted.  

 

I think that the numval = input(val, best.); option of converting is the optimal choice; however, I have come across a situation where this type of conversion is not the best choice.

 

For example, when an algorithm   produces a value that is an imaginary number.   This element needs to be changed so that the rest of the data series can be used for processing.

 

Therefore the numval = val*1; is better method to use.   So I would advocate using numval = val*1; method of converting variable types in cases where there is no other option.

 

many thanks

 

 

 

Super User
Posts: 19,789

Re: Question about automatic variable type conversions character to numeric


yo1 wrote:

 

I think that the numval = input(val, best.); option of converting is the optimal choice; however, I have come across a situation where this type of conversion is not the best choice.

 

For example, when an algorithm produces a value that is an imaginary number. This element needs to be changed so that the rest of the data series can be used for processing.

  


How can you be sure only imaginary numbers are being converted and you don't have any other issues in your algorithm if you multiply by 1? 

 

 

 

Occasional Contributor yo1
Occasional Contributor
Posts: 19

Re: Question about automatic variable type conversions character to numeric

This was a rare case that we came across and that variable in question converted to numeric and the imaginary number changed (I forgot what value it turned into). The conversion took place after the data was processed in the data step.
Solution
‎04-18-2017 10:51 AM
Super User
Posts: 19,789

Re: Question about automatic variable type conversions character to numeric

There is no other reason other than bad practice and sloppy coding.

 

Usually these types of things will mask errors that you won't notice or it will be hard to find actual issues when you have them if you get used to seeing these NOTES and ignoring them. So not coding in this manner will help you in the long run. 

 


yo1 wrote:

Hello,

 

I am trying to find documentation on why one should not mulitply one to a character variable to convert character to numeric (unless its really needed).  I found some information on the paper 248-2012 on page 9 about it; however, I know that there is more information warning others not to perform this method other than saying its bad practice or sloppy coding. 

 

Any resources about not using this method is greatly apprecated.

 

Best


 

 

Occasional Contributor yo1
Occasional Contributor
Posts: 19

Re: Question about automatic variable type conversions character to numeric

Hello Reeza,

 

Thank you for your response.  I accepted it as the solution.  I am still looking for a document that goes into greater detail about the subject.  However, you have brought about a good point about the notes appearing in the log.

 

Massive thanks.

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 136 views
  • 2 likes
  • 3 in conversation