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

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
Barite | Level 11

That works perfectly.

Thank you.

HHC

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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