BookmarkSubscribeRSS Feed
NadiaK
Fluorite | Level 6

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
Reeza
Super User

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

Include what you have and what you need.

Astounding
PROC Star

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

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

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

 

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

WarrenKuhfeld
Ammonite | Level 13

x = n(x);

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

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

NadiaK
Fluorite | Level 6
Wow! Thanks so much!!
Tom
Super User Tom
Super User

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 2054 views
  • 2 likes
  • 6 in conversation