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
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: