SAS (Base SAS)+
data convert;
input id $ char_num $ num_val;
datalines;
1 100 55
2 200 60
3 ABC 65
4 300 .
5 . 75
;
run;
data convert2;
set convert;
* Convert character to numeric;
char_to_num = input(char_num, best.);
* Convert numeric to character;
num_to_char = put(num_val, best.);
run;- SAS splits the two conversion directions across two functions:
INPUT()goes character-to-numeric,PUT()goes numeric-to-character. char_to_num = input(char_num, best.);reads the character value through a numeric informat and returns a numeric result.- The
best.informat picks the most appropriate numeric reading automatically — good enough for most general-purpose conversions. - Invalid strings like
"ABC"cannot be parsed as a number, so SAS returns missing and writes a note to the log. num_to_char = put(num_val, best.);writes the numeric value through a format and returns a character string.- The
best.format gives SAS latitude over width and decimal placement; use something tighter like8.2when the report needs a fixed layout. - We run and confirm
convert2: the ABC row should havechar_to_num = ., and the missing-num_valrow should show an emptynum_to_char.