DATA Step, Macro, Functions and more

Recoding variables

Reply
Occasional Contributor
Posts: 9

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?

 

Super User
Posts: 19,878

Re: Recoding variables

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

Include what you have and what you need.

Super User
Posts: 5,518

Re: Recoding variables

[ Edited ]

How about:

 

varname = (varname ne . );

 

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

 

varname = (varname > .z) ;

Occasional Contributor
Posts: 9

Re: Recoding variables

Posted in reply to Astounding
That's a good idea. Let me try it out!
Trusted Advisor
Posts: 1,586

Re: Recoding variables

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

 

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

SAS Super FREQ
Posts: 309

Re: Recoding variables

x = n(x);

Occasional Contributor
Posts: 9

Re: Recoding variables

Posted in reply to WarrenKuhfeld
I am not sure I understand this. Bear with me, I'm super new at this stuff!
😌
Occasional Contributor
Posts: 9

Re: Recoding variables

Yes!
SAS Super FREQ
Posts: 309

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);

Occasional Contributor
Posts: 9

Re: Recoding variables

Posted in reply to WarrenKuhfeld
Wow! Thanks so much!!
Super User
Super User
Posts: 7,083

Re: Recoding variables


NadiaK wrote:

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);
Ask a Question
Discussion stats
  • 10 replies
  • 145 views
  • 2 likes
  • 6 in conversation