BookmarkSubscribeRSS Feed

LEXCOMBI

Started ‎02-20-2015 by
Modified ‎01-03-2020 by
Views 42,661

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
Version history
Last update:
‎01-03-2020 10:02 AM
Updated by:

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!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Labels
Article Tags