
SAS CONVERT NUMERIC TO CHARACTER PLUS
, 9, a decimal point, and plus or minus signs). In general, the automatic conversion produces a numeric missing value from any character value that does not conform to standard numeric values (containing only digits 0, 1. In case you haven't figured it out yourself, it's the commas in those numbers that is throwing SAS for a loop. Then, you see three NOTES about invalid numeric data concerning the standtest values 1,210, 1,010, and 1,180. The first NOTE that you see is a standard message that SAS prints in the log to warn you that it performed an automatic character-to-numeric conversion on your behalf. NOTE: DATA statement used (Total Process time): NOTE: The dataset WORK.GRADE has 5 observations and 8 variables.

NOTE: Missing values were generated as a result of preforming an operation on missing values.Įach place is givem by: (Number of times) at (Line):(Column). NOTE: Invalid numeric data, standtest='1,180', at line 121 column 11 NOTE: Invalid numeric data, standtest='1,012', at line 121 column 11 NOTE: Invalid numeric data, standtest='1,212', at line 121 column 11 NOTE: Character values have been coverted to numeric values at the places given by: You should see something that looks like this:ġ19 input name $ 1-15 e1 $ e2 $ e3 $ e4 $ standtest $ Launch and run the SAS program, and before looking at the output window, take a look at the log window.

Likewise, when SAS goes to calculate a new standardized test score ( std), SAS first attempts to convert standtest to a numeric variable. Then, when SAS goes to calculate the average exam score ( avg), SAS first attempts to convert e1, e2, e3, and e4 to numeric variables. That is, even the exam scores ( e1, e2, e3, e4) and the standardized test scores ( standtest) are stored as character variables. Okay, first note that for some crazy reason all of the data in the data set have been read in as character data. Input name $ 1-15 e1 $ e2 $ e3 $ e4 $ standtest $ The following SAS program illustrates how SAS tries to perform an automatic character-to-numeric conversion of standtest and e1, e2, e3, and e4 so that arithmetic operations can be performed on them: DATA grades That's why, by the end of our discussion, you'll appreciate that the moral of the story is that it is always best for you to perform the conversions yourself using the INPUT function. The problem with taking this lazy person's approach is that it doesn't always work the way you'd hoped. If you try to do something to a character variable that should only be done to a numeric variable, SAS automatically tries first to convert the character variable to a numeric variable for you. The reality though is that SAS is a pretty smart application. To be specific, we'll learn how the INPUT function converts character values to numeric values. How SAS helps us do that is the subject of this section. In that case, the character variables price and units first need to be converted to numeric variables price and units. Then, you can imagine it being a little odd trying to multiply price by units. It may be the case, however, that price and units are instead stored as character variables. Pretty straightforward, eh? As long as price and units are stored in your data set as numeric variables, then you could just use the assignment statement: sales = price * units


Suppose you are asked to calculate sales income using the price and the number of units sold.
