Hello,
Can anyone tell me how to convert character variable to numeric variable.?
Thank you
Examples of the values involved would help. You cannot change an existing variables data type.
Best is to read the data with an appropriate INFORMAT from the beginning.
Afterwards you can either create a new variable or go through some renaming to get the same variable name associated with a numeric value.
The basic approach is in a data step:
NumericVar = input(CharacterVar,<numericformat.>); The appropriate numeric format may differ for different values though BEST will work for many cases. Suffix the BEST with the largest number of significant digits in your data.
data want;
set have;
Numericvar = input(CharacterVar, best12. ); /* NOTE: the period at the end of the BEST12. is required to let SAS know this is an Informat*/
run;
Examples of the values involved would help. You cannot change an existing variables data type.
Best is to read the data with an appropriate INFORMAT from the beginning.
Afterwards you can either create a new variable or go through some renaming to get the same variable name associated with a numeric value.
The basic approach is in a data step:
NumericVar = input(CharacterVar,<numericformat.>); The appropriate numeric format may differ for different values though BEST will work for many cases. Suffix the BEST with the largest number of significant digits in your data.
data want;
set have;
Numericvar = input(CharacterVar, best12. ); /* NOTE: the period at the end of the BEST12. is required to let SAS know this is an Informat*/
run;
The question is....
The problem Iam having is Iam unable to add salaries corresponding to males and females... The problem I think is due to the dollar sign and comma in the salary variable. I tried to convert these character variable to numeric variable by using input statement as shown below.... but still I am getting errors..
data employee_data1 employee_data2;
set Employee1( keep= gender educ jobcat salary);
input( salary, dollar7.);
if ( gender='m' AND Educ IN( 12 13 14 15) AND jobcat=1) then output bittu.employee_data1;
if ( gender='f' AND Educ IN( 8 9 10 11 12) AND jobcat=1 AND salary > 10,000 ) then output bittu.employee_data2;
run;
proc print;
run;
It might be helpful to provide the example data as it looks in a plain text file or in a SAS printout, as Excel may apply formatting that makes it look different.
In your program fragment, you have at least two errors that will make it not work, even if the data are as they appear in the Excel screenshot.
1. the value returned from the input function needs to be assigned to a variable, like
salary2 = input(salary, dollar7.);
2. when you compare the salary to a particular number in the if statement, that number cannot have commas in it. For example (leaving out the other parts of your if statement for clarity), you want something like:
if salary2 > 10000 then .... ;
Thank you very much Erico..
I finally got my required data..
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.