DATA Step, Macro, Functions and more

identify not missing observations

Reply
New Contributor
Posts: 4

identify not missing observations

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.

ObsVar1Var2Var3notmissing
123.0
23241
3.310
47421

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;

Super User
Super User
Posts: 7,401

Re: identify not missing observations

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.

Valued Guide
Posts: 3,208

Re: identify not missing observations

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.

---->-- ja karman --<-----
Occasional Contributor
Posts: 10

Re: identify not missing observations

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;

Super Contributor
Posts: 275

Re: identify not missing observations

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;

Trusted Advisor
Posts: 1,204

Re: identify not missing observations

data want;

set have;

flag=nmiss(of var1-var3);

run;

New Contributor
Posts: 4

Re: identify not missing observations

Thanks all for your help. For some reason the "and" function wasn't working so just did each variable separately and added them together

Contributor
Posts: 29

Re: identify not missing observations

it should be 'or' not 'and' for the result you need.

Sudeer.

Frequent Contributor
Posts: 117

Re: identify not missing observations

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;

Ask a Question
Discussion stats
  • 8 replies
  • 544 views
  • 7 likes
  • 8 in conversation