Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

converting variable type from character to numeric

Reply
Regular Contributor
Posts: 215

converting variable type from character to numeric

Hi there,

I have the following data set and I wonder if someone can tell me the Code to change the ID column from Character to Numeric so that I can do analysis. My problem is I have some value under ID column that are combination of character and numeric (eg. V13), therefore consider the whole column as text.

The analysis code for my analysis is:

Data Have;

set Out;

where ID between 12 and 24;

run;

NameID
A12
B25
CV13
D24

Thanks,

Super Contributor
Posts: 336

Re: converting variable type from character to numeric

create a new variable using the input function.

some examples here: SAS(R) 9.3 Functions and CALL Routines: Reference

Good luck,

Miguel

Super User
Posts: 10,516

Re: converting variable type from character to numeric

Or try

where ( input( ID,best6.)) between 12 and 24;

though if you expect to get V13 in that result you'll need to do more coding to strip off the character bits before conversion.

Valued Guide
Posts: 858

Re: converting variable type from character to numeric

Do you need to keep the 13 after the V?  If so use the num_id line.  If you want to drop anything that has a character use the new_id line.  Run this and you'll see what I mean:

data have;

infile cards dsd;

input Name$    ID$;

cards;

A,12

B,25

C,V13

D,24

;

data want;

set have;

new_id = input(id,8.);

num_id = input(compress(id,'a','a'),8.);

run;

Ask a Question
Discussion stats
  • 3 replies
  • 364 views
  • 0 likes
  • 4 in conversation