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
Kurt_Bremser
Super User

Use array processing

data want;
set have;
array vars {*} _numeric_;
count = 0;
do i = 1 to dim(vars);
  if vars{i} = -1 then count + 1;
end;
drop i;
run;
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;

 

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 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
  • 1268 views
  • 1 like
  • 3 in conversation