Converting variable types—use PUT() or INPUT()?

8

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:

  1. Is your target variable character or numeric?
  2. Is your source variable character or numeric?
  3. 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

 

 

Share

About Author

Sunil Gupta

Associate Director, Statistical Programming, Cytel

Sunil Gupta is a presenter at SAS Global Forum and a contributor to SAS Users blog. You may reach him at SAS Savvy—Smarter SAS Searches.

Related Posts

8 Comments

  1. 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 = __:)

  2. 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.

  3. 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.

Leave A Reply

Back to Top