Help with Do loop through a list of variables

Accepted Solution Solved
Reply
Super Contributor
Posts: 420
Accepted Solution

Help with Do loop through a list of variables

Hi Everyone,

My data has a list of variable say a b c d e.

I want to create a Do Loop through a combination of this list.

My maincode working with manual combination such as: 

 

%let var1=a;  %let var2=b;

*main code;

 

How could I do something like

 

%let List_variable = a b  c d e;

 

%do i=1 to the end of the list_variable;

%do j=i+1 to the end of the list_variable;

 

I dont know how to turn count (i) into name of variable a,bcde..

 

Thank you for your help.

HHC

 

data have; 
input target a b c d e;
cards;
1 1 2 3 0 6
2 1 2 0 0 6
-1 1 2 0 8 16
-2 2 0 0 8 16
5 0 0 0 8 31
6 0 0 2 2 31
1 1 2 0 1 2
run;

%let var1=a;
%let var2=b;

proc reg data=have ;
   model &var1 = &var2 ;
run;

%let var1=a;
%let var2=c;

proc reg data=have ;
   model &var1 = &var2 ;
run;

 


Accepted Solutions
Solution
‎02-08-2016 08:28 AM
Super User
Posts: 19,869

Re: Help with Do loop through a list of variables

You can use %scan to find the i'th element in the list. You should probably look into Call ALLCOMB though, it's probably an easier way to generate all your 2/3 options and more scalable.

 

%let list=a b c d e;

%macro loop(vlist);
%let nwords=%sysfunc(countw(&vlist));

%do i=1 %to &nwords;
%do j=&i+1 %to &nwords;

%put %scan(&vlist, &i) + %scan(&vlist, &j);

%end;
%end;
%mend;

%loop(&list);

View solution in original post


All Replies
Solution
‎02-08-2016 08:28 AM
Super User
Posts: 19,869

Re: Help with Do loop through a list of variables

You can use %scan to find the i'th element in the list. You should probably look into Call ALLCOMB though, it's probably an easier way to generate all your 2/3 options and more scalable.

 

%let list=a b c d e;

%macro loop(vlist);
%let nwords=%sysfunc(countw(&vlist));

%do i=1 %to &nwords;
%do j=&i+1 %to &nwords;

%put %scan(&vlist, &i) + %scan(&vlist, &j);

%end;
%end;
%mend;

%loop(&list);
Super Contributor
Posts: 420

Re: Help with Do loop through a list of variables

That works perfectly.

Thank you.

HHC

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 6528 views
  • 1 like
  • 2 in conversation