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

14

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

 
WANT MORE GREAT INSIGHTS MONTHLY? | SUBSCRIBE TO THE SAS TECH REPORT

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.

14 Comments

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

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

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

    • Leonid Batkhan

      Hi Victor,

      You can exclude processing invalid values by conditionally executing your INPUT() statement. Here is an example:

      data HAVE;
         length chardate $8;
         chardate = ''; output;
         chardate = 'N_A'; output;
         chardate = '20201002'; output;
         chardate = '20201213'; output;
      run;
      
      data NEW;
         set HAVE;
         if chardate not in ('', 'N_A') then numdate = input(chardate,yymmdd8.);
         format numdate date9.;
      run;
      

      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.

  4. 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;

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

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

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

Leave A Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top