Help using Base SAS procedures

do loops basic question

Reply
Frequent Contributor
Posts: 75

do loops basic question

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
Contributor
Posts: 22

Re: do loops basic question

data step loop? or macro loop?
Contributor
Posts: 22

Re: do loops basic question

Posted in reply to WaltSmith
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]
SAS Super FREQ
Posts: 8,864

Re: do loops basic question

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]
Ask a Question
Discussion stats
  • 3 replies
  • 171 views
  • 0 likes
  • 3 in conversation