Array do loop through a subset of condition

Accepted Solution Solved
Reply
Super Contributor
Posts: 371
Accepted Solution

Array do loop through a subset of condition

I have the following code which loop through all possible combination of 3 factors
data have;
input a1 a2 a3 a4 win_or_loss ;
cards;
1 1 1 1 1 1
1 1 1 1 -1 2
1 1 0 0 1 3
2 1 0 0 1 4
2 0 2 1 -1 5
2 0 2 1 1 6
2 0 3 1 -1 7
run;

 


data want (keep=a_: b_: c_: win_or_loss );
SET have;
array a{*} a1 a2 a3 a4;
array b{*} a1 a2 a3 a4;
array c{*} a1 a2 a3 a4;

do i=1 to dim(a);
do j=i+1 to dim(b);
do k=j+1 to dim(c);
    a_name=vname(a{i});
    b_name=vname(b{j});
    c_name=vname(c{k});
    a_value=a{i};
    b_value=b{j};
    c_value=c{k};
output ww;
end;
end;
end;
run;

 

I want is to limit the first Do loop within a subset of value say (a1= 1 , a1=2 a2=1 , a2=0, a3=2)
(with the setup like that, I know there will be some duplicate say (a1=1 a1=1 a2=2) but it is ok)

 

data want(keep=a_: b_: c_: win_or_loss ID);
SET have;

array a{*} a1 a2 a3 a4;
array b{*}a1 a2 a3 a4;
array c{*}a1 a2 a3 a4;

do (a1= 1 , a1=2 a2=1 , a2=0, a3=2) ; *of course instead of a1=1,2,3.., a2=1,2,3... a3=1,2,3..;
do j=1 to dim(b); *This run full;
do k=j+1 to dim(c); *this run full;
a_name=vname(a{i});
b_name=vname(b{j});
c_name=vname(c{k});

a_value=a{i};
b_value=b{j};
c_value=c{k};

output ww;
end;
end;
end;
run;

 

I appreciate it if you could help me out.

 

HHC


Accepted Solutions
Solution
‎01-29-2016 12:09 AM
Grand Advisor
Posts: 17,289

Re: Array do loop through a subset of condition

I attempted a hash solution, my first one!!!

Test it...thoroughly Smiley Happy

Reference used: http://www.sascommunity.org/wiki/Data_Aggregation_Using_the_SAS_Hash_Object

 

data have;
input a1 a2 a3 a4 win_or_loss ;
cards;
1 1 1 1 1 
1 1 1 1 -1
1 1 0 0 1
2 1 0 0 1
2 0 2 1 -1
2 0 2 1 1
2 0 3 1 -1
;
run;

data _null_;
dcl hash H (ordered: "A");
h.defineKey("a1", "a2", "a3", "a4", "win_or_loss");
h.defineData("a1", "a2", "a3", "a4", "win_or_loss", "count");
h.defineDone();

dcl hash U();
u.definekey("a1", "a2", "a3", "a4", "win_or_loss");
u.definedone();

do until(end);
   set have end=end;
   if h.find() ne 0 then call missing(count);
   count= sum(count, 1);
   
   if u.check() ne 0 then do;
      u.add();
    end;
    h.replace();

     end;
     
     h.output(dataset:"hash_agg");
     stop;
    
run;

data hash_agg;
set hash_agg;
result=ifc(win_or_loss=1, "Win", "Loss");
run;

proc transpose data=hash_agg out=want(drop=_name_);
by a1 a2 a3 a4;
id result;
var count;
run;

proc print data=want;
run;

View solution in original post


All Replies
Grand Advisor
Posts: 17,289

Re: Array do loop through a subset of condition

Ouch. Does it have to be arrays?

 

Maybe proc freq/tabulate are better options? If reporting only, then tabulate definitely, but freq gets you pretty close, just needs a transpose.

 

proc freq data=have noprint;
table a1*a2*a3*a4*win_or_loss/list out=summary;
run;

proc transpose data=summary out=want1 prefix=Score;
by a1 a2 a3 a4;
var count;
id win_or_loss;
run;

/*report only*/
proc tabulate data=have;
class a1-a4 win_or_loss;
table a1*a2*a3*a4, win_or_loss*N="Count"/misstext='0';
run;
Super Contributor
Posts: 371

Re: Array do loop through a subset of condition

I really need it to be array. The other method I try before doesnt work because of the size of the data.

 

Grand Advisor
Posts: 17,289

Re: Array do loop through a subset of condition

Woman Sad

 

That's too bad. Since it's counts they're cumulative, so you could split it up and process...Someone will probably have a hash solution that's better, but I'm not good at that. Hope you find something that works better. 

Solution
‎01-29-2016 12:09 AM
Grand Advisor
Posts: 17,289

Re: Array do loop through a subset of condition

I attempted a hash solution, my first one!!!

Test it...thoroughly Smiley Happy

Reference used: http://www.sascommunity.org/wiki/Data_Aggregation_Using_the_SAS_Hash_Object

 

data have;
input a1 a2 a3 a4 win_or_loss ;
cards;
1 1 1 1 1 
1 1 1 1 -1
1 1 0 0 1
2 1 0 0 1
2 0 2 1 -1
2 0 2 1 1
2 0 3 1 -1
;
run;

data _null_;
dcl hash H (ordered: "A");
h.defineKey("a1", "a2", "a3", "a4", "win_or_loss");
h.defineData("a1", "a2", "a3", "a4", "win_or_loss", "count");
h.defineDone();

dcl hash U();
u.definekey("a1", "a2", "a3", "a4", "win_or_loss");
u.definedone();

do until(end);
   set have end=end;
   if h.find() ne 0 then call missing(count);
   count= sum(count, 1);
   
   if u.check() ne 0 then do;
      u.add();
    end;
    h.replace();

     end;
     
     h.output(dataset:"hash_agg");
     stop;
    
run;

data hash_agg;
set hash_agg;
result=ifc(win_or_loss=1, "Win", "Loss");
run;

proc transpose data=hash_agg out=want(drop=_name_);
by a1 a2 a3 a4;
id result;
var count;
run;

proc print data=want;
run;
Respected Advisor
Posts: 4,948

Re: Array do loop through a subset of condition

A few things stand out about the program.  

 

Why do you need 3 arrays?  They contain the same elements, so one array should be plenty.

 

The way you have coded the DO loops, you are not getting every combination.  You are getting more than that,.  WIth 4 elements in the array, it would be sufficient to code:

 

do i=1 to 2;

   do j=i+1 to 3;

      do k=j+1 to 4;

 

This is just to illustrate ... you don't have to hard-code the stopping values of the loops.  You could always make them depend on dim(a).

 

The easiest way to control which combinations you want in all combination problems (at least in my opinion) is to turn the OUTPUT statement into an IF/THEN/OUTPUT statement.  Run through all the combinations, but output only the ones that you want.

 

I'm not sure I hit on everything you need here, but I hope we're moving in the right direction.

Super Contributor
Posts: 371

Re: Array do loop through a subset of condition

I agree that I need to declare 1 array as below.

 

data ww1 (keep=a_: b_: c_: win_or_loss );
SET have;
array a{*} a1 a2 a3 a4;

do i=1 to dim(a);
do j=i+1 to dim(a);
do k=j+1 to dim(a);
a_name=vname(a{i});
b_name=vname(a{j});
c_name=vname(a{k});
a_value=a{i};
b_value=a{j};
c_value=a{k};
output ww1;
end;
end;
end;

 

But I believe that I have exact all combination.

if you add an ID=_n_

the code below show no duplicate.
proc sort data=want nodupkey;
by a_name b_name c_name a_value b_value c_value id;run;

Respected Advisor
Posts: 4,948

Re: Array do loop through a subset of condition

It you look at the log, it will tell you how many observations were duplicates.  The NODUPKEY option removes them.

Super Contributor
Posts: 371

Re: Array do loop through a subset of condition

Thank you so much.

HHC

☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 506 views
  • 1 like
  • 3 in conversation