BookmarkSubscribeRSS Feed
trekvana
Calcite | Level 5
all-

is there a way to tell a do loop to reference a vector of numbers for its in index? In other words lets say I have a vector index={3 5 6 7 12 3}. I want the do loop to go through i=3,5,6,7,.... instead of 1,2,3,4,5,.....

cheers
3 REPLIES 3
WaltSmith
Fluorite | Level 6
data step loop? or macro loop?
WaltSmith
Fluorite | Level 6
Here's some options:

[pre]
data mydata;
set mydata;
do i=1,3,5,8,6,13;
*-- do stuff with i here --;
put i=;
end;
*-- yet another option --;
do mm='Dec','Jan','Aug','Feb'; *-- no particular order --;
*-- do stuff with mm here --;
put mm=;
end;
run;

%macro myloop;
%let index = 1 3 6 9 2 7;
%do i=1 %to 6; %*best to calculate the dimension of index;
%let idx = %scan( &index, &i, %str( ));
%*-- do stuff with &idx here ---;
%put idx=&idx;
%end;
%mend;
%myloop
[/pre]
Cynthia_sas
Diamond | Level 26
Hi:
In a DATA step program, you can provide a list of numbers or character values to be used in a DATA step DO loop, as shown in the program below. If you were using PROC IML or another STAT procedure that allowed DO loops, you would have to consult that documentation for more information.

cynthia
[pre]
data showdo;
do grp='AAA', 'BBB', 'CCC';
do numvar = 1, 3, 5, 7;
do numvar2 = 3, 9;
output;
end;
end;
end;
run;

proc print data=showdo;
run;
[/pre]

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

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 1650 views
  • 0 likes
  • 3 in conversation