BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

Hello,

Can anyone tell me how to convert character variable to numeric variable.?

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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;

View solution in original post

4 REPLIES 4
ballardw
Super User

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;

preetham_vr386_gmail_com
Calcite | Level 5

The question is....

  1. 1. Create  2 DATA set named employee_data1 and employee_data2. Each data contains 4 variables – Gender, educ, Jobcat & Salary . employee_data1 store only MALE obs having Educ ranges from 12 to 15 and jobcat must be 1  AND employee_data2 store only FEMALE obs having Educ ranges from 8 to 12 , jobcat must be 1 and salary > $10,000
  2. The data of Excel file is......

Untitled.png

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;

Erico
Calcite | Level 5

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 .... ;

preetham_vr386_gmail_com
Calcite | Level 5

Thank you very much Erico..

I finally got my required data..Smiley Happy

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1548 views
  • 4 likes
  • 3 in conversation