BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Rhodochrosite | Level 12

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

2 REPLIES 2
Reeza
Super User

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);
hhchenfx
Rhodochrosite | Level 12

That works perfectly.

Thank you.

HHC

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 57718 views
  • 5 likes
  • 2 in conversation