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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.