2 Comments

December 21, 2019

Variable conversions in SAS refer as to convert numeric to character variables or character to numeric variables.

PUT: Converts numeric to character. The result is a character string the format matches the type of incoming variable.
INPUT: Converts character to numeric and the result is the variable with the same informat.

The PUT and INPUT function for variable conversions in SAS

SAS variables conversions are automatically done in some situations where a numeric variable is used in a character expression or when a character variable is used in a numeric expression and writes a NOTE in the LOG.

data _null_;
a='5';
b=2;
c=a+b;
put c=;
run;
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
       76:3   
 c=7

In the above example, a character variable ‘a’ is used in the numeric expression, therefore SAS has converted the variable to numeric before evaluating the expression. (a+b).

If you have a special character such as “,” to the variable, the automatic conversion fails and an ERROR message is written to the LOG.

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).76:3   
NOTE: Invalid numeric data, a='5,2' , at line 76 column 3.
c=.a=5,2 b=2 c=. ERROR=1 N=1

Therefore, it is a good programming practice to manually convert the variable to its required type.

You can use the PUT and INPUT functions to directly convert from numeric to character and character to numeric.

Additionally, you can use a format with these functions. The PUT function is used to convert from numeric to the character and the INPUT function is used to convert from character to numeric.

Character to Numeric variable conversions in SAS

Character variables are converted to a numeric format. A numeric infomat causes the INPUT function to return a numeric value. The selection of the informat depends on the form of the character date.

If you have any special character in the variable, you can use the applicable informat during the conversion as in the example below.

data _null_;
a='5,2';
b=2;
c=input(a,comma.)+b;
put c=;
run;

Numeric to Character variable conversions in SAS

The PUT function is generally used to convert a numeric value to a character. Since a numeric format is used, the resulting string is right-justified. The LEFT function is then applied to left justify the string.

data _null_;
a=10023;
b="*"||put(a,8.)||"*";
put b=;
c="*"||left(put(a,8.))||"*";
put c=;
run;
b=*   10023*
c=*10023   *

PUTN, PUTC, INPUTN, and INPUTC

Variable conversion ins SAS using the INPUT or PUT are applied during the DATA step compilation phase.

To apply the informat or format during the DATA step execution phase you need to use the INPUTC, INPUTN, PUTC, or PUTN functions.

PUTN and PUTC function returns the formatted value using the specified format.

In INPUTC function, the source_data is a character, the result data is also a character and the informat that we use is the
character informat.

data Example;
 /*INPUTC - Source- Character, Informat-Character, Result-Character*/
 /*INPUTN - Source- Character, Informat-Numeric,   Result-Character*/
 /*PUTC   - Source- Charcater, Format- Character,  Result-Character*/
 /*PUTN  - Source- Numeric,   Format-Numeric,     Result-Numeric*/
 name='Hello World';
 date='10dec2019';
 Group='New';
 price=2000;
 name1=inputc(name, '$upcase30.');
 date1=inputn(date, 'date7.');
 Group1=putc(group, '$QUOTE6.');
 Price1=putn(price, 'comma');
run;

The main function of INPUTC/INPUTN is to convert a character with a given informat to a character or numeric data.
The PUTC/PUTN function is to convert a character/numeric data into a character data in a given particular format.

Note that, INPUT and INPUTC trim trailing blanks whereas PUT and PUTC preserve blanks.

related posts:


Column Input in SAS to Read Raw Data Arranged in Columns


Date Interval Functions – INTNX and INTCK in SAS


SAS Generation Datasets

Subhro Kar

About the author

Been in the realm with the professionals of the IT industry. I am passionate about Coding, Blogging, Web Designing and deliver creative and useful content for a wide array of audience.

Subhro

Leave a Reply

Your email address will not be published. Required fields are marked

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

  1. We stumbled over here different web page and thought I might check things out.
    I like what I see so now i am following you. Look forward to exploring your web
    page again.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}