[prev in list] [next in list] [prev in thread] [next in thread] 

List:       sas-l
Subject:    Re: SAS Forum: Creating Sum Variables For All Combinations Of 9 Variables
From:       S=?UTF-8?Q?=C3=B8ren_Lassen?= <000002b7c5cf1459-dmarc-request () LISTSERV ! UGA ! EDU>
Date:       2017-09-27 9:18:10
Message-ID: 8715991911405664.WA.s.lassenpost.tele.dk () listserv ! uga ! edu
[Download RAW message or body]

Roger,
You can make things a lot faster by noting that GRAYCODE() does minimal changes to \
the array (only one element is changed at a time), and that the return code marks \
which element is changed: data have;
infile cards dlm=',';
input h1-h9;
cards;
1,2,4,8,16,32,64,128,256
;
run;
data want;
  set have;
  array h{*} h1-h9;
  array x{*} x1-x9;
  k=-1;
  sum=0;
  rc=graycode(k,of x{*});
  output;
  do i=1 to 2**dim(x)-1;
    rc=graycode(k,of x{*});
    if x{rc} then
      sum=sum+h{rc};
    else
      sum=sum-h{rc};
    output;
    end;
  keep h1-h9 sum k;
run;

Regards,
Søren

On Mon, 25 Sep 2017 16:22:32 -0400, Roger DeAngelis <rogerjdeangelis@GMAIL.COM> \
wrote:

> SAS Forum: Creating Sum Variables For All Combinations Of 9 Variables
> 
> WORKING CODE
> ============
> 
> kSharp  ( best solution)
> 
> array h{*} h1-h3;  * input data;
> array x{*} x1-x3;  * 1/0 matrix to sum the correct combination;
> k=-1;
> do i=1 to 2**dim(x);
> rc=graycode(k,of x{*}); * Generates all subsets of 3 items in a minimal change \
> order; sum=0;                  * each time gets the proper row in the 0/1 matrix;
> do j=1 to dim(x);
> sum+x{j}*h{j};        * graycode row of 0/1 times data;
> end;
> output;
> end;
> 
> 
> SAS/IML/R WPS/Proc R  (could not figure out how to remove the loop - slow?)
> ============================================================================
> 
> bin <- rep(list(0:1), nco);
> grd<-expand.grid(bin);       * 0/1 matrix;
> for (i in 1:nro) {
> want<-rbind(want,as.matrix(rowSums(mat[i,] * grd)));
> };
> import r=want data=wrk.wantwps;
> 
> set have;
> do rec=0 to 7;
> set wantwps;
> bin=put(rec,binary3.);
> output;
> end;
> 
> see
> https://goo.gl/MPtVV7
> https://communities.sas.com/t5/General-SAS-Programming/Creating-Sum-Variables-For-All-Combinations-Of-9-Variables/m-p/397972
>  
> kSharp profile
> https://communities.sas.com/t5/user/viewprofilepage/user-id/18408
> 
> HAVE
> 
> SD1.HAVE total obs=2
> 
> Obs    H1    H2    H3
> 
> 1      2     2     2
> 2      3     3     3
> 
> 
> DETAILS
> 
> Obs    H1    H2    H3           RULES         SUM
> 
> 1      2     2     2        0     0     0    0*2+0*2+0*2 = 0
> 1     0     0    1*2+0*2+0*2 = 2
> 1     1     0    1*2+1*2+0*2 = 4
> 0     1     0    ...
> 0     1     1
> 1     1     1
> 1     0     1
> 0     0     1
> 
> WANT
> 
> kSharp
> 
> WORK.WANT total obs=16
> 
> Obs    X1    X2    X3    H1    H2    H3    SUM
> 
> 1     0     0     0     2     2     2     0
> 2     1     0     0     2     2     2     2
> 3     1     1     0     2     2     2     4
> 4     0     1     0     2     2     2     2
> 5     0     1     1     2     2     2     4
> 6     1     1     1     2     2     2     6
> 7     1     0     1     2     2     2     4
> 8     0     0     1     2     2     2     2
> 9     0     0     0     3     3     3     0
> 10     1     0     0     3     3     3     3
> 11     1     1     0     3     3     3     6
> 12     0     1     0     3     3     3     3
> 13     0     1     1     3     3     3     6
> 14     1     1     1     3     3     3     9
> 15     1     0     1     3     3     3     6
> 16     0     0     1     3     3     3     3
> 
> 
> My solution R
> 
> Up to 40 obs WORK.WANT total obs=16
> 
> Obs    BIN    H1    H2    H3    SUM
> 
> 1    000     2     2     2     0
> 2    001     2     2     2     2
> 3    010     2     2     2     2
> 4    011     2     2     2     4
> 5    100     2     2     2     2
> 6    101     2     2     2     4
> 7    110     2     2     2     4
> 8    111     2     2     2     6
> 9    000     3     3     3     0
> 10    001     3     3     3     3
> 11    010     3     3     3     3
> 12    011     3     3     3     6
> 13    100     3     3     3     3
> 14    101     3     3     3     6
> 15    110     3     3     3     6
> 16    111     3     3     3     9
> 
> *                _               _       _
> _ __ ___   __ _| | _____     __| | __ _| |_ __ _
> > '_ ` _ \ / _` | |/ / _ \   / _` |/ _` | __/ _` |
> > > > > > > (_| |   <  __/  | (_| | (_| | || (_| |
> > _| |_| |_|\__,_|_|\_\___|   \__,_|\__,_|\__\__,_|
> 
> ;
> 
> options validvarname=upcase;
> libname sd1 "d:/sd1";
> data sd1.have;
> input h1-h3;
> cards4;
> 2 2 2
> 3 3 3
> ;;;;
> run;quit;
> 
> 
> *_     ____  _
> > > __/ ___|| |__   __ _ _ __ _ __
> > > / /\___ \| '_ \ / _` | '__| '_ \
> > <  ___) | | | | (_| | |  | |_) |
> > _|\_\|____/|_| |_|\__,_|_|  | .__/
> > _|
> ;
> 
> data want;
> retain x1-x3;
> set sd1.have;
> array h{*} h1-h3;
> array x{*} x1-x3;
> k=-1;
> do i=1 to 2**dim(x);
> rc=graycode(k,of x{*});
> sum=0;
> do j=1 to dim(x);
> sum+x{j}*h{j};
> end;
> output;
> end;
> keep x1-x3 h1-h3 sum;
> run;
> 
> *                      ____
> __      ___ __  ___   |  _ \
> \ \ /\ / / '_ \/ __|  | |_) |
> \ V  V /| |_) \__ \  |  _ <
> \_/\_/ | .__/|___/  |_| \_\
> > _|
> ;
> 
> 
> * number of columns=3;
> * number of rows=2;
> 
> %let ncol=3;
> %let nrow=2;
> 
> 
> %utl_submit_wps64(resolve('
> libname sd1 "d:/sd1";
> options set=R_HOME "C:/Program Files/R/R-3.4.0";
> libname wrk "%sysfunc(pathname(work))";
> proc r;
> submit;
> source("C:/Program Files/R/R-3.4.0/etc/Rprofile.site", echo=T);
> library(haven);
> library(tidyr);
> mat<-as.matrix(read_sas("d:/sd1/have.sas7bdat"));
> mat;
> nro=&nrow;
> nco=&ncol;
> cmb=2**nco;
> bin <- rep(list(0:1), nco);
> grd<-expand.grid(bin);
> want <- matrix(, nrow = 0, ncol = 1);
> for (i in 1:nro) {
> want<-rbind(want,as.matrix(rowSums(mat[i,] * grd)));
> };
> endsubmit;
> import r=want data=wrk.wantwps;
> run;quit;
> '));
> 
> 
> data want (rename=v1=sum);
> retain bin;
> set sd1.have;
> do rec=0 to 7;
> set wantwps;
> bin=put(rec,binary3.);
> output;
> end;
> drop rec;
> run;quit;


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic