Hi:
Look in the documentation for the topics:
Functions and CALL Routines
INPUT Function
The bottom line is that to convert a CHARACTER variable to a number, you use the INPUT function like this:
newnumvar = INPUT(oldcharvar,numeric-informat.); (DATA step) OR INPUT(oldcharvar,numeric-informat.) as newnumvar (SQL)
So you were ALMOST right in your INPUT statement. Think of it this way, in order to turn your old character variable into a NEW numeric variable, you have to give the NUMERIC INformat to use.
See the example below.
cynthia
** make some data;
** ssnc is a 9 position character variable with SSN;
** ssn is a numeric variable;
data testit;
name = 'alan';
ssn = 123456789;
ssnc = '123456789';
output;
name = 'bob';
ssn = 002334444;
ssnc = '002334444';
output;
run;
ods listing;
proc sql ;
select name, ssn format=ssn11., ssnc,
input(ssnc,9.) as newssn format=ssn11.
from work.testit;
quit;
and the output:
name ssn ssnc newssn ----------------------------------------- alan 123-45-6789 123456789 123-45-6789 bob 002-33-4444 002334444 002-33-4444
Hi:
Look in the documentation for the topics:
Functions and CALL Routines
INPUT Function
The bottom line is that to convert a CHARACTER variable to a number, you use the INPUT function like this:
newnumvar = INPUT(oldcharvar,numeric-informat.); (DATA step) OR INPUT(oldcharvar,numeric-informat.) as newnumvar (SQL)
So you were ALMOST right in your INPUT statement. Think of it this way, in order to turn your old character variable into a NEW numeric variable, you have to give the NUMERIC INformat to use.
See the example below.
cynthia
** make some data;
** ssnc is a 9 position character variable with SSN;
** ssn is a numeric variable;
data testit;
name = 'alan';
ssn = 123456789;
ssnc = '123456789';
output;
name = 'bob';
ssn = 002334444;
ssnc = '002334444';
output;
run;
ods listing;
proc sql ;
select name, ssn format=ssn11., ssnc,
input(ssnc,9.) as newssn format=ssn11.
from work.testit;
quit;
and the output:
name ssn ssnc newssn ----------------------------------------- alan 123-45-6789 123456789 123-45-6789 bob 002-33-4444 002334444 002-33-4444
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.