How many times have you had a need to convert between variable types such as converting character to numeric or numeric to character? For example, what if you have a character variable with numeric values but you need to perform some calculations? Or, if you have a numeric variable but you need to concatenate it to a character variable? If you are like most SAS programmers, you need to use PUT() and INPUT() at least once to complete these tasks.
The answer to the question "Do I use PUT() or INPUT()?" depends on what your target variable type is and what your source variable type and data are. Below are three questions to consider:
- Is your target variable character or numeric?
- Is your source variable character or numeric?
- If your source variable is character, is your data value character or numeric?
Based on your answers to the three questions above, you can identify whether PUT() or INPUT() comes first. Keep these four rules in mind when writing your SAS statements:
- PUT() always creates character variables
- INPUT() can create character or numeric variables based on the informat
- The source format must match the source variable type in PUT()
- The source variable type for INPUT() must always be character variables
The following examples show how to use these rules to convert from character/numeric or numeric/character:
A PUT() converts character variable to another character variable.
B PUT() converts numeric variable to a character variable with numeric value.
C PUT() converts character variable with a user defined format to another character variable.
D INPUT() converts character variable with numeric value and informat to a numeric variable.
E INPUT() converts character variable with numeric value and informat to a character variable.
F INPUT() converts character variable with numeric value and informat to a numeric variable.
Function Call | Raw Type | Raw Value | Returned Type | Returned Value |
A PUT(name, $10.); | char, char format | ‘Richard’ | char always | ‘Richard ’ |
B PUT(age, 4.); | num, num format | 30 | char always | ‘ 30’ |
C PUT(name, $nickname.); | char, char format | ‘Richard’ | char always | ‘Rick’ |
D INPUT(agechar, 4.); | char always | ‘30’ | num, num informat | 30 |
E INPUT(agechar, $4.); | char always | ‘30’ | char, char informat | ‘ 30’ |
F INPUT(cost,comma7.); | char always | ‘100,541’ | num, num informat | 100541 |
WANT MORE GREAT INSIGHTS MONTHLY? | SUBSCRIBE TO THE SAS TECH REPORT
15 Comments
Hello. Thanks for this simplifying post.
Is this a possible typo: in example "E", it states: "INPUT() converts character variable with numeric value and informat to a character variable." Should that say "...numeric value and character informat..."?
Thanks again,
- Joe
COMO PUEDO hacer una exportación de SAS a Excel si tengo datos alfanuméricos de este tipo xxl00009 , ya que al usar proc export se pierden datos y en Excel salen datos diferentes, gracias .
Hola me gustaría saber como exportar un archivo SAS a Excel lo realice con proc export , pero mi archivo Sas tiene una columna con datos alfanuméricos (kxl00009) por lo que dice que se han quitados datos en la exportación , como le puedo hacer . gracias 🙂
Thank you so much, it helped a lot.
hello
I am try to convert character field to numeric. This character field contains dates however some of the fields are either blank or have N_A.. How do I get around this error message pls
NOTE: Invalid argument to function INPUT at line 38 column 27.
Hi Victor,
You can exclude processing invalid values by conditionally executing your INPUT() statement. Here is an example:
In this case, numeric values corresponding to invalid character date values will be missing, and no message "NOTE: Invalid argument to function INPUT" will be generated.
Hi,
A character informat (type=J) can be used with a numeric variable in an input() function can be used to create a character variable containing numeric values.
A character informat (type=J) can also be used with a character variable in an input() function to create a character variable containing character values.
In this last instance, what is the benefit of using this method over a put() function having a character variable as first argument and a character format as second argument?
It there any specific scenario where it would give a different result?
proc format;
value $demo 'A'='ABC';
invalue $test 'A'='ABC';
run;
data x;
x='A';
y=input(x,$test.);
z=put(x,$demo.);
run;
proc print data=x;
run;
please i need to convert several variable from character to numeric.
LIBNAME XXX 'c:\users\wagdy';
my data name is xxx.data218a;
one of the variables i want to Change is called PW.
please what should i do. i would appreciate your help.
Thank you so much,
Helped a lot in my work.
For years I have used the LENGTH statement to convert a numeric variable to character, or vice versa, so simple I feel sneaky! Here's the little bit of code:
**assume varx is the original numeric variable you want to convert to char**;
DATA final; LENGTH $ varx; SET oldfile(RENAME=(varx=vartemp));
varx = vartemp; drop vartemp; run;
**on the log will be a warning that you are using a numeric variable to create a character, but it will proceed nicely. You can certainly reverse this, by stating the LENGTH statement for a numeric variable.
Do you have any easy tricks for preserving the variable name when you convert it? Right now I do this (assume x is numeric and a is character):
data new (drop = __:)
Ann,
You can rename your variable in the SET statement, then create a new variable with the original name afterwards. For example:
data new (drop x_character);
set original_dataset (rename=(x = x_character));
format x 8.0;
x = input(x_character,$20.); /*replace "$20." with the original format of X */
run;
Here is how you can preserve variable name and position on the data set - Changing variable type and variable length in SAS datasets.
Having an easy way to remember whether it is put or input is something I grappled with when learning SAS and came up with a little trick that I share with students I teach. Check it out in the blog post http://blogs.sas.com/content/sastraining/2013/02/26/rhymes-mnemonics-and-tips-in-learning-sas/
Thanks Michelle for the tip.