BookmarkSubscribeRSS Feed
sagarwal1
Fluorite | Level 6
First to convert a character variable into number, user needs to store that numeric variable into a new variable. It can be done by two methods:
-- multiply by 1 ( Character var * 1) --> convert the char var into a numeric variable
Eg - new_var = char var * 1
-- use Sas inbuilt method INPUT.
Syntax - INPUT ( Var name, w.d);
hashman
Ammonite | Level 13

@sagarwal1:

Methinks using the "multiplication method" is a poor practice: It results in an implicit character-to-numeric type conversion and pollutes the log with a corresponding note "Character values have been converted to numeric values...". Besides, if the input is not a valid number, it will pollute the log even more with two more notes: "Invalid numeric data..." and "Missing values were generated...".

 

It's always much cleaner to use the INPUT function with a corresponding informat preceded by the ?? modifier. This way, you get a clean log without any vagaries and can test the result for a missing value if need be to find out if the informat has failed to interpret the input as a number or not.

 

Kind regards

Paul D.       

sagarwal1
Fluorite | Level 6
@hashman
I appreciate for guiding in what will be the consequences of using multiplication method to convert character variable to numerical variable. I tried using this method to convert into number and it worked. So I suggested others to use it but I personally use INPUT method to get rid of unexpected problems. Once again, thanks for your clarification.
ChrisNZ
Tourmaline | Level 20

> I tried using this method to convert into number and it worked. 

You can only say "it worked" if you disregard message in the log. And you should never ignore log messages.

A good program generates a clean log. A bad program doesn't.

 

xxformat_com
Barite | Level 11

I fully agree on the fact clean log belongs to good programming practices. A reason for that is that when a real bug occurs, we want to make sure to only get messages related to that bug to fix it. It makes life easier.

 

Having said that, the input function returs a numeric value only when a numeric informat is used, not when a character informat is is used.

 

Finally, here is an example where an input function does not just convert from character to numeric! So, be careful!

 

data _null_;
    x =input('200.',10.6);
    y1=input('200',10.6);
    y2=200/1000000;
    
    put x=;
    put y1=;
    put y2=;
run;
Kurt_Bremser
Super User

In informats, the decimal fraction (the "d" value in all format documentations) should only be used when no decimal separator is supplied, but a defined number of fractional digits is used. Typical in mainframe data files where 000012345 often means 123 dollars and 45 cents.

 

JulieLearnsSAS
Fluorite | Level 6
One thing that helped me with typing was copying code out of a book. I read the text that I wanted to copy in the book, and typed it on my computer. My eyes were either on the book, or the monitor. That process made me a much more efficient typist and I found myself far less frequently looking at the keyboard. Once I became better at typing, I found that I could work a lot more fluidly and much more painlessly.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 22 replies
  • 15540 views
  • 72 likes
  • 12 in conversation