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?
Can you post sample data to illustrate what you're trying to accomplish?
Include what you have and what you need.
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) ;
Assuming the present variable is called VAR - do you mean:
if missing(var) then code=0; else code=1;
x = n(x);
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 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);
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.
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.