BookmarkSubscribeRSS Feed
achris
Calcite | Level 5

Hi there,

I have 12 numeric variables (values of either 0,1,2) an am hoping to create a single variable which counts the number of "1"s or "2"s across rows (I have over 20 000 obs); e.g. how many 2s or 1s are in each row/record (rows can have both 1 or 2 - so wont be able to divide out) - I know there must be a simple way of doing this but haven't had much luck yet - any help would be greatly appreciated? Thanks so much!

3 REPLIES 3
Reeza
Super User

Have you tried an array and do loop or are you looking for something different?

array vars(12) var1-var12;

var_count=0;

do i=1 to dim(vars);

if vars(i) in (1,2) then var_count+1;

end;

data_null__
Jade | Level 19

Peek?

data _012;
   array v[12];
   do id = 1 to 10;
     
do _n_ = 1 to dim(v);
         v[_n_]=rantbl(123,.3,.3)-1;
        
end;
        
output;
     
end;
  
run;
proc print;
  
run;
data count;
   retain id;
   array v[12];
   set _012;
   c0 = count(peekclong(addrlong(v[1]),dim(v)*8),put(0,rb8.));
   c1 = count(peekclong(addrlong(v[1]),dim(v)*8),put(1,rb8.));
   c2 = count(peekclong(addrlong(v[1]),dim(v)*8),put(2,rb8.));
   run;
proc print;
  
run;

3-10-2015 12-09-03 PM.png  
Haikuo
Onyx | Level 15

Another one Smiley Happy,

data countc;

set have;

array v v1-v12;

ctof12=countc(cats(of v(*)),'12');

run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1659 views
  • 0 likes
  • 4 in conversation