SAS character to numeric or SAS numeric to character
sas convert character to numeric, c, sas numeric to character, convert character to numeric sas, convert numeric to character sas, sas character to numeric, character to numeric sas, numeric to character sas, sas change character to numeric, sas convert numeric to string, sas put input, sas input function, sas put function
You might be interested in this course:
When you’re a new SAS programmer, understanding how to convert between SAS data types is vital. In light of that fact, it is not surprising that a lot of people google ‘sas character to numeric’ or ‘sas numeric to character’. With this in mind, there are a couple of SAS functions that help us convert between data types. Specifically, the INPUT and PUT functions help us perform these conversions. If you are more of a visual learner, I have a full course available covering this and many other sas base topics SAS Online Tutor for Base Programming: Learn SAS
SAS Convert Character To Numeric – INPUT FUNCTION
To begin, the input function allows us to convert character to numeric variable. First, lets look at the syntax of the input function.
INPUT(source, <?|??> informat.)
The source argument or parameter is where you ‘pass’ or enter a character constant, variable, or expression that you want to convert. The informat, the second argument, converts the value of the source argument. In other words, an informat will read the source value (first argument/parameter), and then convert it.
Now, the way an informat converts, or what convert actually comes down to, is quite simple. The usual case is that we have some variable, let’s say a variable named ‘sales’, that has commas or other characters in it. As a result of that, when we declare our variables in our input statement (not to confuse input statement with input function), the commas and/or characters, force us to declare ‘sales’ as a character variable.
But now, we want to do calculations or some form of statistical analysis on that sales variable. In this scenario, we have to convert it to a numeric variable. All the informat does for us is get rid of the character(s) (ie the comma in 345,500). That is what convert entails in this scenario.
Now, an informat like comma9. (the 9 refers to the width of the input field..the range is 1 to 32) will get rid of commas, dollar signs, dashes, percent signs, and a few other things. If you have just a comma, then the informat comma9. will work just fine. Therefore, input=(sales,comma9.);. Don’t forget the period/dot at the end of an informat.
Finally, I’m sure you noticed the question marks as part of the function. The question marks are optional modifiers that you may use to stop the printing of error messages when invalid data values are being read.
SAS convert numeric to character – PUT Function
The syntax for the Put function, when you want to convert a numeric variable to a character variable, is not much different.
The only real difference here is that you are applying a format as the second argument.
Using Both Functions At The Same Time
One important thing to keep in mind is that you can use both at the same time. There are a few scenarios where it makes sense to do so.
One example would be using the put function to first convert a numeric character to a character variable, then use the input function and convert that now character data type to another character type or a date. Yes, you read that right. You can use the input function to covert a character value to another character value by using a different character informat than the source, or you can use a date informat in the second argument to turn that character value into a date value.
To end this, I hope you have found this article useful. If you have any questions, feel free to ask in the comments!
You might be interested in these articles: