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-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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