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;

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