Change character values to numeric

Accepted Solution Solved
Reply
Contributor
Posts: 65
Accepted Solution

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

View solution in original post


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

Respected Advisor
Posts: 3,799

Re: Change character values to numeric

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

PROC Star
Posts: 7,467

Re: Change character values to numeric

Posted in reply to data_null__

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: 7,467

Re: Change character values to numeric

Posted in reply to kaushal2040

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

Contributor
Posts: 50

Re: Change character values to numeric

Thanks, Art.

Contributor
Posts: 65

Re: Change character values to numeric

Thanks Linlin so much!

Issac

🔒 This topic is solved and locked.

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

Discussion stats
  • 8 replies
  • 328 views
  • 1 like
  • 5 in conversation