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;
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);
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);
That works perfectly.
Thank you.
HHC
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.