BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jiangmi
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
stat_sas
Ammonite | Level 13

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

7 REPLIES 7
Haikuo
Onyx | Level 15

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

ballardw
Super User

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;

Ksharp
Super User

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

stat_sas
Ammonite | Level 13

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;

PGStats
Opal | Level 21

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
Peter_C
Rhodochrosite | Level 12

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

jiangmi
Calcite | Level 5

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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