We’re smarter together. Learn from this collection of community knowledge and add your expertise.

LEXCOMBI

by Respected Advisor on ‎02-20-2015 08:41 AM - edited on ‎10-05-2015 03:30 PM by Community Manager (283 Views)

The thread "Need help with sas macros"  https://communities.sas.com/thread/71429 which has little to do with macro, got me to thinking about how this is a nice example of an application of LEXCOMBI.

 

Using LEXCOMBI allows the program to be parameterized with regard to the variables processed and the number of variables that are added together.

 

data test;
   array _v
  • A B C D E (
1:5);
   do _n_ = 1 to 10;
     
do _i_ = 1 to dim(_v);
         _v[_i_] = _v[_i_] * 2;
        
end;
     
output;
     
end;
  
drop _:;
   run;
proc print;
  
run;

*parameters;
%let varlist=A-numeric-E;
%let k = 3;

data score;
   retain _Type_ 'SCORE';
  
length _Name_ $32;

  
if 0 then set test(keep=&varlist); *get variables list into array;
  
array _varlist
  • &varlist; 

      
array _i[&k] _temporary_; *dim(_varlist) choose dim(_i);
   __n_ = dim(_varlist);
   __k_ = dim(_i);
   __ncomb_ = comb(__n_,__k_);
  
do __j_ = 1 to __ncomb_;
      __rc_ = lexcombi(__n_,__k_,of _i
  • );
         
call missing(_name_,of _varlist
  • );
      do __h_ = 1 to dim(_i);
         __ii_ = _i[__h_]; *VNAME does not accept double subscript reference;
         _name_ = catx(
'_',_name_,vname(_varlist[__ii_]));
         _varlist[__ii_] = 1;
        
end;
     
output;
     
end;
  
stop;
  
drop __:; *double leading underscore so we can drop easily;
  
run;

proc print;
  
run;
proc score data=test score=score out=scored;
   var a--e;
   run;
proc print;
  
run;
 
2-20-2015 7-46-57 AM.png
Your turn
Sign In!

Want to write an article? Sign in with your profile.