Sample 24590: Convert values from character to numeric or from numeric to character
Convert variable values using either the INPUT or PUT
function.
Click on the Full Code tab to see the examples.
/* Convert a character value to a numeric value by using the INPUT */
/* function. Specify a numeric informat that best describes how to */
/* read the data value into the numeric variable. */
/* */
/* When changing types a new variable name is required. If you need */
/* to keep the original variable name for the new type, use the */
/* RENAME= option as illustrated in Sample 2. */
data char;
input string :$8. date :$6.;
numeric=input(string,8.);
sasdate=input(date,mmddyy6.);
format sasdate mmddyy10.;
datalines;
1234.56 031704
3920 123104
;
proc print;
run;
/* Convert a numeric value to a character value by using the PUT */
/* function. Specify a numeric format that describes how to write */
/* the numeric value to the character variable. To left align */
/* the resulting character value, specify -L after the format */
/* specification. */
/* */
/* When changing types a new variable name is required. If you need */
/* to keep the original variable name, use the RENAME= option on */
/* the SET statement to rename the variable as it comes into the PDV. */
/* This allows the original variable name to be reused when you */
/* change type. */
data now_num;
input num date: mmddyy6.;
datalines;
123456 110204
1000 120504
;
data now_char;
set now_num (rename=(num=oldnum date=olddate));
num=put(oldnum,6. -L);
date=put(olddate,date9.);
run;
proc print;
run;
Sample 1: INPUT function
Obs string date numeric sasdate
1 1234.56 031704 1234.56 03/17/2004
2 3920 123104 3920.00 12/31/2004
Sample 2: PUT function
Obs oldnum olddate num date
1 123456 16377 123456 02NOV2004
2 1000 16410 1000 05DEC2004
Convert variable values using either the INPUT or PUT
function.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Functions ==> Special
|
| Date Modified: | 2009-06-04 09:36:19 |
| Date Created: | 2004-09-30 14:08:56 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |