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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.