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

Hi Everyone,

 

For each ID, I want to count the number of positive value for each variable ( a,b ..c) seperately. 

Yes, I can do the proc means as below 1 by 1 and sql them back but should it be a better way to get the job done.

Could you please help?

Thank you,

HHC

 


data have;
input ID a b c d e f g ;
datalines;
1 1 0 -1 6 9 5 3 -9 6
1 2 3 6 0 0 0 -2 -2 -9
1 2 3 6 0 0 0 -2 -2 -9
2 5 0 -1 6 -9 -5 3 9 6
2 4 -3 -6 0 0 0 -2 2 -9
2 -2 3 6 0 0 0 -2 -2 9
;
/*for variable a*/
proc means data=have;
by id;
where a>0;
var a;
output out=want_A(drop=_TYPE_ _FREQ_) 
n=N_A_positive;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Or a different approach.

 

data tab;
   set have;
   array v(*) a b c d e f g;
   do i = 1 to dim(v);
      v[i] = (v[i]>0);
   end;
   drop i;
run;

proc summary data=tab;
   var a b c d e f g;
   output out=want (drop= _:) sum=;
run;

View solution in original post

4 REPLIES 4
stat_sas
Ammonite | Level 13

First transpose the data set then use sql to get positive values for each of the classification.

 

data want(keep=name val);
set have;
array v(*) a b c d e f g;
do i=1 to dim(v);
val=v(i);
name=vname(v(i));
output;
end;
run;

 

proc sql;
select name,sum(val>0) as freq_pos from want
group by name;
quit;

ballardw
Super User

Or a different approach.

 

data tab;
   set have;
   array v(*) a b c d e f g;
   do i = 1 to dim(v);
      v[i] = (v[i]>0);
   end;
   drop i;
run;

proc summary data=tab;
   var a b c d e f g;
   output out=want (drop= _:) sum=;
run;
PGStats
Opal | Level 21

WIDE, being the *wrong* dataset format, again...

 

data have;
input ID a b c d e f g ;
datalines;
1 1 0 -1 6 9 5 3 -9 6
1 2 3 6 0 0 0 -2 -2 -9
1 2 3 6 0 0 0 -2 -2 -9
2 5 0 -1 6 -9 -5 3 9 6
2 4 -3 -6 0 0 0 -2 2 -9
2 -2 3 6 0 0 0 -2 -2 9
;

data have0;
set have;
obs = _n_;
run;

proc transpose data=have0 out=have1;
by ID obs;
var a -- g;
run;

data have2;
set have1;
positive = col1 > 0;
run;

proc sql;
create table want as
select
    ID,
    _name_ as var,
    sum(positive) as N_Positive
from have2
group by ID, var;
quit;
PG
Astounding
PROC Star

A single DATA step can do the trick:

 

data want;

array positive {7};

array nums {7} a b c d e f g;

do _n_=1 to 7;

   positive{_n_} = 0;

end;

do until (last.id);

   set have;

   by id;

   do _n_=1 to 7;

      if nums{_n_} > 0 then positive{_n_} + 1;

   end;

end;

do until (last.id);

   set have;

   by id;

   output;

end;

run;

 

The top loop counts the positives, and the bottom loop outputs the results on each observation for that ID.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 4 replies
  • 3927 views
  • 0 likes
  • 5 in conversation