Solved
Contributor
Posts: 65

# Change character values to numeric

Hi Folks;

Am working with 9.3 version. I got a column containing character values, say MARRIED NEVER_MARRIED, and want to convert them into numbers. I tried this with proc format and format statements but It does not work. Here is the code:

proc new;

set old;

proc format;

values \$marfor 'MARRIED' = 1

'NEVER_MARRIED' = 2;

format marital \$mar. ;

run;

Thanks!

Issac

Accepted Solutions
Solution
‎08-14-2012 02:41 PM
Super Contributor
Posts: 1,636

## Re: Change character values to numeric

try:

data old;
length marital \$ 20;
marital='MARRIED';
output;
marital='NEVER_MARRIED';
output;
run;

proc format;
invalue \$ marfor 'MARRIED' = 1

'NEVER_MARRIED' = 2;

data new;
set old;
newmarital=input(marital,\$marfor.);
proc print;run;

Obs    marital          newmarital

1     MARRIED              1
2     NEVER_MARRIED        2

All Replies
Solution
‎08-14-2012 02:41 PM
Super Contributor
Posts: 1,636

## Re: Change character values to numeric

try:

data old;
length marital \$ 20;
marital='MARRIED';
output;
marital='NEVER_MARRIED';
output;
run;

proc format;
invalue \$ marfor 'MARRIED' = 1

'NEVER_MARRIED' = 2;

data new;
set old;
newmarital=input(marital,\$marfor.);
proc print;run;

Obs    marital          newmarital

1     MARRIED              1
2     NEVER_MARRIED        2

Posts: 3,852

## Re: Change character values to numeric

The new variable is TYPE=Character I thought the OP wanted a numeric variable.

PROC Star
Posts: 8,164

## Re: Change character values to numeric

DN: Agreed!  But it wouldn't require much to change Linlin's suggested code:

data old;

length marital \$ 20;

marital='MARRIED';

output;

marital='NEVER_MARRIED';

output;

run;

proc format;

invalue marfor 'MARRIED' = 1

'NEVER_MARRIED' = 2;

run;

data new;

set old;

newmarital=input(marital,marfor.);

run;

Super Contributor
Posts: 1,636

## Re: Change character values to numeric

Hi DN and Art,

Thank you very much for the correction!

Contributor
Posts: 50

## Re: Change character values to numeric

Hi Linlin,

when  to use invalue or value in proc format?  I always use value statement in proc format?

PROC Star
Posts: 8,164

## Re: Change character values to numeric

Invalue is used to create an informat, while value is used to create a format.

Contributor
Posts: 50

Thanks, Art.

Contributor
Posts: 65

## Re: Change character values to numeric

Thanks Linlin so much!

Issac

🔒 This topic is solved and locked.