DATA Step, Macro, Functions and more

Instead of MISS()/CMISS(), wanna detect Negative values.

Accepted Solution Solved
Reply
Contributor
Posts: 61
Accepted Solution

Instead of MISS()/CMISS(), wanna detect Negative values.

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


Accepted Solutions
Solution
‎01-01-2015 01:38 PM
Trusted Advisor
Posts: 1,228

Re: Instead of MISS()/CMISS(), wanna detect Negative values.

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;

View solution in original post


All Replies
Respected Advisor
Posts: 3,156

Re: Instead of MISS()/CMISS(), wanna detect Negative values.

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 scoreSmiley Happy;

     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

Super User
Posts: 11,343

Re: Instead of MISS()/CMISS(), wanna detect Negative values.

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;

Super User
Posts: 10,035

Re: Instead of MISS()/CMISS(), wanna detect Negative values.

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

Solution
‎01-01-2015 01:38 PM
Trusted Advisor
Posts: 1,228

Re: Instead of MISS()/CMISS(), wanna detect Negative values.

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;

Respected Advisor
Posts: 4,927

Re: Instead of MISS()/CMISS(), wanna detect Negative values.

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

PG
Valued Guide
Posts: 2,177

Re: Instead of MISS()/CMISS(), wanna detect Negative values.

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

Contributor
Posts: 61

Re: Instead of MISS()/CMISS(), wanna detect Negative values.

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 1138 views
  • 8 likes
  • 7 in conversation