data have;
input id$ score1 score2 score3;
cards;
A 12 23 0
B 34 -5 41
C -8 56 -9
;run;
data want;
input id$ score1 score2 score3;
cards;
A 12 23 0
;run;
I need know if any of the variables (score1-score3) contains negative values and delete the obs with any negative value in it.
Now, I can do it with an array.
My question is: initially I was thinking about finding some functions similar to MISS()/CMISS() that will evaluate negative value situation for all the variables at once.
Does anybody know anything about it?
Thank you and Happy New Year.
Joe
data have;
input id$ score1 score2 score3;
if smallest(1,of score1-score3)>=0;
cards;
A 12 23 0
B 34 -5 41
C -8 56 -9
;
run;
I am not aware of any existing function does just that, array() would be a text book approach, but you can also do:
data have;
input id$ score1 score2 score3;
_a=cats(of score:);
if length(compress(_a,'-'))=length(_a);
drop _a;
cards;
A 12 23 0
B 34 -5 41
C -8 56 -9
;
run;
Happy New Year!
Haikuo
Or the MIN function. Returns lowest value of listed variables and returns missing ONLY when all are missing.
data have;
input id$ score1 score2 score3;
if min(score1,score2,score3)<0 then delete;
/* if you want to keep the obs when all of the scores are missing, assuming that may happen then
if . < min(score1,score2,score3)<0 then delete;
*/
cards;
A 12 23 0
B 34 -5 41
C -8 56 -9
;
run;
You could also check SIGN() to judge if it is negative.
data have; input id $ score1 score2 score3; s=sign(score1);put score1= s=; s=sign(score2);put score2= s=; s=sign(score3);put score3= s=; cards; A 12 23 0 B 34 -5 41 C -8 56 -9 ; run;
Xia Keshan
data have;
input id$ score1 score2 score3;
if smallest(1,of score1-score3)>=0;
cards;
A 12 23 0
B 34 -5 41
C -8 56 -9
;
run;
Depends if you want to drop the all-missing observations or not :
data have;
input id$ score1 score2 score3;
if smallest(1, of score1-score3, 1) >= 0;
if n(of score1-score3) > 0; /* If you want to drop all-missing obs */
cards;
A 12 23 0
B 34 -5 41
C -8 56 -9
D . . .
E . 1 2
F . -1 2
;
PG
Consider the simple case - NO values are missing.
Then the simple statement would be
where min( of _numeric_) GE 0 ;
That drops all obs where ANY numeric value is negative
Hi, All,
Thanks for your inputs. They all make sense to my situation and some of you even went further to include missing values. I appreciate that.
It looks like there is no existing function to find the negative values directly, however, people find ways to get around. and there are a lot of ways to choose from.
It is definite another learning experience for me.
Joe
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.