Eliminate one variable on all others

I have a SAS dataset with hundreds of variables. I want to take the tenth variable and regress it on all the others, something like

   proc reg data=mydata;
      model [10th one] = [all the others];
   run;

      

Is there an easy way to do this without typing a complete list of variables? I can imagine a terribly tedious solution in which I (a) go through the 10th variable name and manually enter it on the left side, and (b) use an sql statement to put all other variables in a macro variable for the right side.

+3


source to share


3 answers


Simplifying your method with dictionary.columns

. This is completely identical to the method proc contents

in what ultimately happens, it just makes it take less code and a little less time.

proc sql;
  select name into :reg_varlist separated by ' '
    from dictionary.columns
    where libname='WORK' and memname='MYDAT'
      and name ne "&tenth_Variable";
quit;

proc reg data=mydat;
   model &tenth_variable = &reg_varlist;
run;

      

You can trivially wrap this in a macro if you like, with tenth_variable

as a parameter.



If you really want to regress the "tenth variable" in particular, and don't care what it called, then you can use varnum

.

Here is a macro that does this in a basic form. The regression modeled below is probably completely pointless, and it makes no effort to defend against the chosen character variables; you probably want to add this to ( and type="num"

for example).

%macro regress_onevar(varnum=,dataset=,libname=work);
    proc sql;
      select name into :reg_varlist separated by ' '
        from dictionary.columns
        where libname=upcase("&libname.") and memname=upcase("&dataset.")
          and varnum ne &varnum.;    *varnum=position in dataset;

      select name into :tenth_variable
        from dictionary.columns
        where libname=upcase("&libname.") and memname=upcase("&dataset.")
          and varnum eq &varnum.;
     quit;

    proc reg data=&libname..&dataset.;
       model &tenth_variable = &reg_varlist;
    run;
%mend regress_onevar;

%regress_onevar(varnum=10,dataset=usecon,libname=sashelp);

      

+2


source


Let's assume you know the following variables: 1 - Var_1st 9th - Var_9th 11 - Var_11th Nth - Var120th

proc reg data=mydata;
  model [10th one] = var_1st--var_9th var_11th--var_120th;
run;quit;

      



If you want to completely automate the use of sashelp.vcolumn:

proc sql noprint;
select name into :reg_list separated by " " from sashelp.vcolumn where upper(libname)='WORK' and    upper(memname)='MYDATA' and varnum ne 10;
select name into :dep_list from sashelp.vcolumn where upper(libname)='WORK' and upper(memname)='MYDATA' and varnum eq 10;
quit;

proc reg data=mydata;
model &dep_list = &reg_list;
run;quit;

      

+3


source


Here's the best I've found so far, which assumes you take the trouble to find the name of the 10th variable:

proc contents data=mydat(drop=[name of 10th variable]) out=varnames(keep=name) noprint; 
run;
proc sql noprint;
   select name into : reg_varlist separated by ' '
   from varnames;
quit;
run;

proc reg data=mydat;
   model [name of 10th variable]) = &reg_varlist;
run;

      

+2


source







All Articles