BookmarkSubscribeRSS Feed
K_S
Obsidian | Level 7 K_S
Obsidian | Level 7

The database that I am working with records lab values as character variables beacuse lab techs sometimes write notes/comments instead of enter a numeric lab results.

I have been converting this variable to numeric by adding a 0 to it (i.e. character_var+0) ...this seems to do the trick, but am wondering if there are any downsides to doing this. I am only interested in the numeric measurements so other than ending up with missing fields where there were notes/comments are there any downsides to doing this?

14 REPLIES 14
JoshB
Quartz | Level 8

This is known as implicit type conversion, basically having SAS automatically convert a character to a numeric and you'll likely have a note in the log that this is happening. It is normally considered bad programming practice and should be avoided. The normal character to numeric conversion in SAS is done through the INPUT function. If you know the format of the numeric lab values, you can use INPUT(lab value,informat.). If it is somewhat variable (sometimes 4 characters, sometimes 13, etc.) you can use the BEST. informat. References for the INPUT function and the BEST informat are available in SAS documentation if you want more background.

K_S
Obsidian | Level 7 K_S
Obsidian | Level 7
unfortunately that is not an option, I have no ways of inputting the values
(or are just unaware of how it could be done).
Thank you for your feedback!

##- Please type your reply above this line. Simple formatting, no
attachments. -##
PaigeMiller
Diamond | Level 26

@K_S wrote:
unfortunately that is not an option, I have no ways of inputting the values
(or are just unaware of how it could be done).


The recommendation was not that you input the values. The recommendation was that you use the SAS function INPUT instead of adding zero to the value.

--
Paige Miller
K_S
Obsidian | Level 7 K_S
Obsidian | Level 7
hmmmm....i have to look into how to do this. Thanks!
ballardw
Super User

Some organizations with strict code management policies will require "clean" log results meaning no errors, warnings and sometimes even no notes. This process may cause a violation of that policy.

 

Personally if that were my data I would probably address this sort of issue at the data read step and either create to variables, If the notes were needed later or read as numeric to begin with and suppress the resulting "invalid data" messages that are going to insue.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

I can only agree with the other posters.  This uses implicit (i.e. your not specifying it, your letting the system guess it) conversion.  Always a bad technique.  Always make sure you - the person closest to it - is in complete control.  Use the input() function.

Astounding
PROC Star

While I generally agree, here are some tools to help cope with the situation.

 

numval = input(charval, ??20.);

 

This will convert the existing values to their numeric equivalent, if possible.  However, adding ?? will suppress messages about invalid data if the original set of characters are not numeric.

 

For cleaning the data, or perhaps being more rigorous about what can be converted and what can't, you could try:

 

proc freq data=labdata;

tables charval;

where charval > ' ' and input(charval, ??20.) = .;

run;

 

This will give you a table of all the values that can't be converted, so you can inspect them and see if there is something you might be able to do with them.

amol2939
Calcite | Level 5
Hello
My data lines is as follow
Data abc;
Input a $ b;
Data lines;
Normal 0(0.0)
Run;

Can anyone let me know how to convert it into numeric format
PaigeMiller
Diamond | Level 26

What would be the resulting numeric from this conversion? 

--
Paige Miller
amol2939
Calcite | Level 5
 
amol2939
Calcite | Level 5
normal 0(0.0)
PaigeMiller
Diamond | Level 26

@amol2939 wrote:
normal 0(0.0)

This is not numeric. Can you explain further?

--
Paige Miller
amol2939
Calcite | Level 5
 
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Please open new threads for new questions in future.

 

0 is a number

( is not a number

0.0 is a number

) is not a number

 

To get the two separate numbers then you need two variables, and string parse the string you have:

data abc;
  input a $ b $;
  c=input(scan(b,"(",1),best.);
  d=input(compress(scan(b,"(",2),")"),best.);
data lines;
Normal 0(0.0)
run;

c takes the first number (i.e. the text before the opening bracket) and converts it to numeric using best format.

d takes the second number (i.e. the text after the opening bracket), removes the closing bracket, then converts to number using best format.

 

I assume this is from clinical outputs where 0 is the count, and 0.0 is the percentage of population, you should be refering to the underlying data, not the produced output!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 14 replies
  • 4411 views
  • 0 likes
  • 7 in conversation