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
Rhodochrosite | Level 12

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
Rhodochrosite | Level 12

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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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