Fluorite | Level 6

## Recoding variables

Hello all,

I am trying to recode a variable containing numbers in decimal form. Whatever number is in that variable, I would like it to be coded as 1 and the missing values to be coded as 0. Does anyone have a quick easy solution?

10 REPLIES 10
Super User

## Re: Recoding variables

Can you post sample data to illustrate what you're trying to accomplish?

Include what you have and what you need.

PROC Star

## Re: Recoding variables

varname = (varname ne . );

In light of subsequent comments ... this approach can handle special missing values with just a slight change:

varname = (varname > .z) ;

Fluorite | Level 6

## Re: Recoding variables

That's a good idea. Let me try it out!
Garnet | Level 18

## Re: Recoding variables

Assuming the present variable is called VAR - do you mean:

if missing(var) then code=0; else code=1;

Rhodochrosite | Level 12

## Re: Recoding variables

x = n(x);

Fluorite | Level 6

## Re: Recoding variables

I am not sure I understand this. Bear with me, I'm super new at this stuff!
😌
Fluorite | Level 6

## Re: Recoding variables

Yes!
Rhodochrosite | Level 12

## Re: Recoding variables

The n function returns the number of nonmissing arguments.  There are 28 types of missing values, so direct comparisons to '.' might work for most data sets, but that does not provide a general solution.  Solutions involving the N and NMISS functions work for any type of missing.  Also, one does not need an IF/ELSE sequence when the goal is to create a single variable.  If the result is Boolean, just assign the result 0 or 1 to a value.  X = (X ne .); is a perfectly fine assignment statement that avoids IF/ELSE (although again, it does not provide a general solution).  Also see IFN  and IFC.  This is harder than it needs to be for your problem, but it again avoids using two statements where one works.  x = ifn(n(x), 1, 0);

Fluorite | Level 6

## Re: Recoding variables

Wow! Thanks so much!!
Super User

## Re: Recoding variables

Hello all,

I am trying to recode a variable containing numbers in decimal form. Whatever number is in that variable, I would like it to be coded as 1 and the missing values to be coded as 0. Does anyone have a quick easy solution?

The MISSING() function will return 1 (true) if the value is missing and 0 (false) if not. So you just want

``x = not missing(x);``
Discussion stats
• 10 replies
• 1437 views
• 2 likes
• 6 in conversation