Help re: Converting a character variable to numerical variable

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

Help re: Converting a character variable to numerical variable

I have a number of character variables with 'yes' and 'no'  answers and with missing values for each of those character variables. I have to do 2 things with these.

1. I need to fix those missing values with 'no' values.

if variable='.' then variable= 'No'; ---does not work ! Any idea how else I should use this code?

2. I need to convert those character variable to a numerical variable with just 1 or 0 values and of course coverting all missing variable to 0 value.

Is there an easier way to do that rather than having to create a new variable?

I tried below to create a new variable, and it works just fine. But, I have few hundreds of these variables and wondering if there is an easier way to change character variable to numerical variable rather than having to create a new variable?

  if postretrobleed='Yes' then retrobleed=1;

else if postretrobleed='No' then retrobleed=0;

else if postretrobleed=. then retrobleed = 0;

Your help is truly appreciated !


Venki


Accepted Solutions
Solution
‎10-08-2012 07:16 PM
Super User
Posts: 5,518

Re: Help re: Converting a character variable to numerical variable

Venki,

There are a few things going wrong here.

The message about invalid numeric data means that your original variable is not character, but is numeric.

The proper way to refer to a numeric missing value would not use quotes:  if numvar=. then do;

The proper way to refer to a character missing value would not use a dot:  if charvar=' ' then do;

If you are willing to convert "Yes" to 1 and any other possible value to "No" then you don't need to change missings to "No".  This statement would do the trick:  postretrobleed=(retrobleed='Yes');

Note, however, that this is case-sensitive.  If retrobleed is "YES" or "yes", you will end up with 0 instead of 1.

Good luck.

View solution in original post


All Replies
Contributor
Posts: 21

Re: Help re: Converting a character variable to numerical variable

When I do the

if variable='.' then variable= 'No'; then it returns this:

Invalid numeric data, Variable='No' , at line 125 column 4

Solution
‎10-08-2012 07:16 PM
Super User
Posts: 5,518

Re: Help re: Converting a character variable to numerical variable

Venki,

There are a few things going wrong here.

The message about invalid numeric data means that your original variable is not character, but is numeric.

The proper way to refer to a numeric missing value would not use quotes:  if numvar=. then do;

The proper way to refer to a character missing value would not use a dot:  if charvar=' ' then do;

If you are willing to convert "Yes" to 1 and any other possible value to "No" then you don't need to change missings to "No".  This statement would do the trick:  postretrobleed=(retrobleed='Yes');

Note, however, that this is case-sensitive.  If retrobleed is "YES" or "yes", you will end up with 0 instead of 1.

Good luck.

Contributor
Posts: 21

Re: Help re: Converting a character variable to numerical variable

Posted in reply to Astounding

Terrific !

Thank you so much...

Venki

Super Contributor
Posts: 282

Re: Help re: Converting a character variable to numerical variable

Hi Venki,

FYI, if you ever have another scenario in which you need to check if a variable value is missing and Astounding's neat solution is not appropriate to the situation, then you could try using the missing() function. E.g.:

if missing(variablename) then

It does not matter if the variable you are testing is character or numeric, missing() will return a 1 (true) if the argument is missing and 0 (false) otherwise.

Regards,

Amir.

Contributor
Posts: 21

Re: Help re: Converting a character variable to numerical variable

Thanks Amir !

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 339 views
  • 4 likes
  • 3 in conversation