BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AZIQ1
Quartz | Level 8

Hi,

Have 

data;

var1 var2 var3 var4

-1     20    4      -1

60   -1     -1      -1;

run;

 

want;

var1 var2 var3 var4   want              (counts number of vars with -1)

-1     20    4      -1         2

60   -1     -1      -1;        3

 

I have some vars with -1 value, I want to count how many vars have -1 values. Any ideas? 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Use an array and loop over the array count how many members equal minus one.

data want;
  set have;
  array x var1-var4 ;
  want=0;
  do i=1 to dim(x);
    want + (x(i)=-1);
  end;
run;

But if you changed your coding and replaced -1 with missing value (or one of the 27 special missing values) then you could use NMISS() function.  Plus you could use other functions like MIN(),MAX(), MEAN() on the set of variables since they would ignore the missing values.

data have;
  input var1-var4 ;
cards;
 . 20  4  .
60  .  .  .
;

data want;
  set have ;
  want = nmiss(of var1-var4);
run;

 

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Use an array and loop over the array count how many members equal minus one.

data want;
  set have;
  array x var1-var4 ;
  want=0;
  do i=1 to dim(x);
    want + (x(i)=-1);
  end;
run;

But if you changed your coding and replaced -1 with missing value (or one of the 27 special missing values) then you could use NMISS() function.  Plus you could use other functions like MIN(),MAX(), MEAN() on the set of variables since they would ignore the missing values.

data have;
  input var1-var4 ;
cards;
 . 20  4  .
60  .  .  .
;

data want;
  set have ;
  want = nmiss(of var1-var4);
run;

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 991 views
  • 1 like
  • 3 in conversation