Typecast character to numeric – INPUT() and numeric to character – PUT() SAS

In order to typecast character to numeric in SAS we will be using INPUT() function. To typecast numeric to character in SAS we will be using PUT() function. Let’s see an example of type conversion or casting of numeric column to character column and character column to numeric column in SAS.

  • Typecast a numeric column to character column in SAS using PUT() Function
  • Typecast a character column to numeric column in SAS using INPUT() Function

So we will be using EMP_DET Table in our example

Typecast character to numeric - INPUT and numeric to character - PUT() SAS 1

 

 

Convert numeric to character in SAS – PUT() Function

PUT() Function takes column name as argument and converts the column from numeric to character column in SAS.

/* numeric to character - PUT() */ 

data EMP_DET; 
set EMP_DET; 
DISTRICT_CHAR = PUT(District,best.); 
run;

So the table along with character converted column will beTypecast character to numeric - INPUT and numeric to character - PUT() SAS 2

 

 

Convert Character to Numeric in SAS – INPUT() Function : Method 1

INPUT() Function takes column name as argument and converts the column from character to numeric column in SAS.

/* character to numeric -  INPUT() METHOD 1 */ 
 
data EMP_DET1; 
set EMP_DET; 
DISTRICT_INT = INPUT(DISTRICT_CHAR,best.); 
run; 

So the table along with numeric converted column will beTypecast character to numeric - INPUT and numeric to character - PUT() SAS 3

 

 

Convert Character to Numeric in SAS : Method 2

In order to convert character column to numeric column in SAS in a simple way we can just multiply the character column by 1 which will result in numeric column as shown below

/*   character to numeric - METHOD 2 */ 
 
data EMP_DET1; 
set EMP_DET; 
DISTRICT_INT = DISTRICT_CHAR * 1; 
run;

So the table along with numeric converted column will be
Typecast character to numeric - INPUT and numeric to character - PUT() SAS 4

 

                                                                               

DataScience Made Simple