0
votes

I have stolen a bit of code from elsewhere to create all combinations of variables. I need this to create multiple regressions and then determine the best. I like the output as I can then use a line and have all the names of the variables in 1 place.

The array works when I enter the data manually, but this needs to work across different data and self select the variables so I need to use a macro variable to input the data. This shouldn't be a problem, this works with other datasteps. Can someone tell me where I'm going wrong.

data test(keep=my_string);
length my_string $200.;
  array a[4] $ ('new1' 'new2' 'new3' 'new4');

  n = dim(a);

   do k=1 to n;
         do j=1 to comb(n,k);
             call allcomb(j,k,of a[*]);
                 do i = 1 to k;
                    if i=1 then do; my_string="";counter=0;end;
                    counter=counter+1;
                    my_string=catx(" ",my_string, a[i]);
                 if counter=k then output;

                 end;
           end;
    end;
run;

This next element doesn't work. Just gives me missing values - but it knows it needs 127... subs is just a macro variable with new1-new7 in it.

rsubmit;
data xx(keep=my_string);
length my_string $200.;
  array a &subs;

  n = dim(a);

   do k=1 to n;
         do j=1 to comb(n,k);
             call allcomb(j,k,of a[*]);
                 do i = 1 to k;
                    if i=1 then do; my_string="";counter=0;end;
                    counter=counter+1;
                    my_string=catx(" ",my_string, a[i]);
                 if counter=k then output;

                 end;
           end;
    end;
run;
endrsubmit;

Your help is very much appreciated.
J

2

2 Answers

1
votes

If you have defined subs as

%let subs=new1-new7;

Then SAS thinks these are variables, not string values. If you delete that keep= statement you will see that SAS created variables new1-new7.

You need to keep it in the format from the first example. Try this:

%let subs='new1' 'new2' 'new3' 'new4' 'new5' 'new6' 'new7';
%let n=7;
data xx(keep=my_string);
length my_string $200.;
  array a[&n] $ (&subs);

  n = dim(a);

   do k=1 to n;
         do j=1 to comb(n,k);
             call allcomb(j,k,of a[*]);
                 do i = 1 to k;
                    if i=1 then do; my_string="";counter=0;end;
                    counter=counter+1;
                    my_string=catx(" ",my_string, a[i]);
                 if counter=k then output;

                 end;
           end;
    end;
run;

If you want to use the form you had, then you have to read the name of the variable in the array and use that. Here I am am creating a new array of strings to hold the names. You can just change the value of n and see this will work for all values (until you run out of space in my_string which I upped in size):

%let n=7;
%let subs=new1-new&n;

data xx(keep=my_string);
length my_string $1000.;
  array v &subs;
  array a[&n] $32. _temporary_;

  n = dim(v);

  do i=1 to n;
     a[i] = vname(v[i]); 
  end;

   do k=1 to n;
         do j=1 to comb(n,k);
             call allcomb(j,k,of a[*]);
                 do i = 1 to k;
                    if i=1 then do; my_string="";counter=0;end;
                    counter=counter+1;
                    my_string=catx(" ",my_string, a[i]);
                 if counter=k then output;

                 end;
           end;
    end;
run;
0
votes

Sorry for any waste of time. The answer was that my array was without speech marks, so I had to recreate it with the speech marks in the array. Then it works like a charm.

Where anz is the number in the macro.

rsubmit;
data hasitreallyworked;
length my_string $200.;
  array a[&anz] $ (&subs2); 

  n = dim(a);

   do k=1 to n;
         do j=1 to comb(n,k);
             call allcomb(j,k,of a[*]);
                 do i = 1 to k;
                    if i=1 then do; my_string="";counter=0;end;
                    counter=counter+1;
                    my_string=catx(" ",my_string, a[i]);
                 if counter=k then output;

                 end;
           end;
    end;
run;
endrsubmit;