Data conversion

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Data conversion

Hello,

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

Thank you


Accepted Solutions
Solution
‎02-04-2015 05:34 PM
Super User
Posts: 11,144

Re: Data conversion

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


All Replies
Solution
‎02-04-2015 05:34 PM
Super User
Posts: 11,144

Re: Data conversion

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;

New Contributor
Posts: 3

Re: Data conversion

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;

Occasional Contributor
Posts: 5

Re: Data conversion

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

New Contributor
Posts: 3

Re: Data conversion

Thank you very much Erico..

I finally got my required data..Smiley Happy

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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