Convert Between Numeric and Character Variables¶
Converting between numeric and character variables is one of the most frequently encountered issues when processing datasets. This article explains how to do this conversion correctly and efficiently.
Numeric to Character¶
Assume there's an imported dataset named
cik is stored as a
numeric variable as shown below:
cik is of different digits, to convert the numeric
cik into a
character variable, the natural procedure is to pad it with leading zeros. For
cik (Central Index Key) itself is a 10-digit number used by SEC.
In SAS, convert numeric variable to string with leading zeros (assuming 10-digit
fixed length) is done via
PUT() function also works in
cik_char variable is of format and informat
$10., and the
In STATA, convert numeric variable to string with leading zeros (assuming
6-digit fixed length) can be achieved via the
Character to Numeric¶
In SAS, converting a character variable to a numeric one uses the
In STATA, this conversion be can be done via either
real() function or
real() function works on a single variable.
destring command can convert
all character variables into numeric in one go.
If a character variable has non-numeric characters in it, then it will not be
converted. In such a case, you may choose to use the
although it in fact is generating categories.
A more detailed explanation with examples is available at stats.idre.ucla.edu