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
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;
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;
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.