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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

 

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

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;

 

sas-innovate-2024.png

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.

 

Register now!

How to connect to databases in SAS Viya

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.

Discussion stats
  • 2 replies
  • 802 views
  • 1 like
  • 3 in conversation