I want to create a new variable to show which rows have no missing observations. Rows with no missing variables are coded as 1 in the notmissing variable and 0 otherwise
i.e.
Obs | Var1 | Var2 | Var3 | notmissing |
---|---|---|---|---|
1 | 2 | 3 | . | 0 |
2 | 3 | 2 | 4 | 1 |
3 | . | 3 | 1 | 0 |
4 | 7 | 4 | 2 | 1 |
The code that I have is not working. This is what I have tried:
data work.dataset
set work.dataset
if var1^=. and var2^=. var3^=. then notmissing=1;
else notmissing=0;
run;
Hi,
Change you code to (as you are missing an and:
if var1 ne . and var2 ne . and var3 ne . then notmissing=1;
else notmissing=0;
Also, if you are using SAS 9.2 then you can use the CMISS() function and if > 0 then 1 else 0. There are other ways too, check out arrays if you have lots of variables to check, then you could use a do loop to go over it.
data have;
attrib var1 var2 var3 notmissing format=best.;
var1=2; var2=3; var3=.; output;
var1=3; var2=2; var3=4; output;
run;
data want (drop=i);
set have;
array var{3};
notmissing=1; /* Assume not missing */
do i=1 to 3;
if var{i} = . then notmissing=0; /* if any are missing then set to missing */
end;
run;
The benefit of the above becomes clear when you have 10 or 20 var variables, as you just need to increase the loop size.
SAS(R) 9.4 Functions and CALL Routines: Reference, Second Edition (NMISS function)
Using with variable lists you will get easy code. Just all variables being non-missing will give a value of 0 and all with some missings above.
hi,
the code which you have written is perfectly alright just you missed out one 'AND' operator between var2,var3.
corrected statement:-
if var1 ne . and var2 ne . and var3 ne . then notmissing=1;
data have;
input id var1-var3;
cards;
1 2 3 .
2 3 2 4
3 . 3 1
4 7 4 2
;
run;
data want;
set have;
missing_flag=(nmiss(of var1-var3)=0);
run;
data want;
set have;
flag=nmiss(of var1-var3);
run;
Thanks all for your help. For some reason the "and" function wasn't working so just did each variable separately and added them together
it should be 'or' not 'and' for the result you need.
Sudeer.
Hi,,
Hope OR should work instead of AND ;
data work.dataset;
set work.dataset;
if Var1 = . OR Var2 = . OR Var3=. then notmissing = 0 ;
else notmissing = 1;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.