Running regression for a list of Dependent variable

Accepted Solution Solved
Reply
Super Contributor
Posts: 402
Accepted Solution

Running regression for a list of Dependent variable

Hi Everyone,

The code below help me to run 05 regression for dependent variable v1-v5.

My problem is that my actual data has dependent variables of name not follow any rule (say a34d xhi3..)

Can you please help me with the code to run the macro with actual random name?

Thank you,

HHC

data have;
  input v1-v5 ind1 ind2;

     *actual input name;

     *     input ab cd gh ff1 st2 indi ind2;
  cards;
1 0 1 1 0 34 23
0 0 1 0 1 22 32
1 1 1 0 0 12 10
0 1 0 1 1 56 90
0 1 0 1 1 26 80
1 1 0 0 0 46 45
0 0 0 1 1 57 53
1 1 0 0 0 22 77
0 1 0 1 1 44 45
1 1 0 0 0 41 72
;
run;

%macro mylogit(num);
   %do i = 1 %to #
      proc logistic data=have des;
        model v&i = ind1 ind2;
      run;
  %end;
%mend;
*executing the macro using 5 dependent variable;
%mylogit(5)


Accepted Solutions
Solution
‎12-05-2014 09:49 AM
Trusted Advisor
Posts: 1,789

Re: Running regression for a list of Dependent variable

If you know the names of the independent variables, the you could use PROC CONTENTS to obtain the names of ALL variables, and then eliminate the independent variables, leaving the dependent variables.

UNTESTED CODE

proc contents data=have noprint out=_cont_;

run;

proc sql noprint;

     select distinct name into :dependents separated by ' ' from _cont_ where name not in ('ind1','ind2');

quit;

%macro mylogit(dependents);
   %do i = 1 %to %sysfunc(countw(&dependents,%str( )));

     %let thisdependent=%scan(&dependents,&i,%str( ));
      proc logistic data=have des;
        model &thisdependent = ind1 ind2;
      run;
  %end;
%mend;

%mylogit(&dependents)

View solution in original post


All Replies
Respected Advisor
Posts: 3,788

Re: Running regression for a list of Dependent variable

Transpose and use BY statement.

data have;
  input ab cd gh ff1 st2 ind1 ind2;
  id = _n_; *necessary evil;
    
*actual input name;
    
*     input ab cd gh ff1 st2 indi ind2;
 
cards;
1 0 1 1 0 34 23
0 0 1 0 1 22 32
1 1 1 0 0 12 10
0 1 0 1 1 56 90
0 1 0 1 1 26 80
1 1 0 0 0 46 45
0 0 0 1 1 57 53
1 1 0 0 0 22 77
0 1 0 1 1 44 45
1 1 0 0 0 41 72
;
run;
proc transpose data=have out=thave name=dependent;
   by id ind:;
   var ab--st2;
   run;
proc sort data=thave;
   by dependent;
   label dependent='Dependent Variable';
  
run;
proc logistic data=thave des;
  by dependent;
  model col1 = ind1 ind2;
  run;
Solution
‎12-05-2014 09:49 AM
Trusted Advisor
Posts: 1,789

Re: Running regression for a list of Dependent variable

If you know the names of the independent variables, the you could use PROC CONTENTS to obtain the names of ALL variables, and then eliminate the independent variables, leaving the dependent variables.

UNTESTED CODE

proc contents data=have noprint out=_cont_;

run;

proc sql noprint;

     select distinct name into :dependents separated by ' ' from _cont_ where name not in ('ind1','ind2');

quit;

%macro mylogit(dependents);
   %do i = 1 %to %sysfunc(countw(&dependents,%str( )));

     %let thisdependent=%scan(&dependents,&i,%str( ));
      proc logistic data=have des;
        model &thisdependent = ind1 ind2;
      run;
  %end;
%mend;

%mylogit(&dependents)

Super Contributor
Posts: 402

Re: Running regression for a list of Dependent variable

Thank you,

If I know the name of the variables, can I simply create a list of them?

Trusted Advisor
Posts: 1,789

Re: Running regression for a list of Dependent variable

If I know the name of the variables, can I simply create a list of them?

Yes, that works too (if you mean a macro variable containing the names).

Super Contributor
Posts: 402

Re: Running regression for a list of Dependent variable

I try to put the list of variables like:

%let dependents ={v1 , v2, v3};

and messing around with your code but cannot get it go

Could you help me to fix it?

Thank you,

HHC

Trusted Advisor
Posts: 1,789

Re: Running regression for a list of Dependent variable

The syntax for a list inside a macro variable is very simple

%let dependents=v1 v2 v3;

Super User
Posts: 19,145

Re: Running regression for a list of Dependent variable

You can also call it with the values hardcoded:


%mylogit(v1 v2 v3);

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 288 views
  • 6 likes
  • 4 in conversation