BookmarkSubscribeRSS Feed
janie99
Calcite | Level 5

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;

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

jakarman
Barite | Level 11

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 --<-----
Dalveer
Calcite | Level 5

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;

slchen
Lapis Lazuli | Level 10

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;

stat_sas
Ammonite | Level 13

data want;

set have;

flag=nmiss(of var1-var3);

run;

janie99
Calcite | Level 5

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

Chrishi
Calcite | Level 5

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

Sudeer.

Vish33
Lapis Lazuli | Level 10

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 8 replies
  • 3176 views
  • 7 likes
  • 8 in conversation