DATA Step, Macro, Functions and more

do loops and name appending

Reply
Contributor
Posts: 47

do loops and name appending

hi all,

some help?

data newtable;
do i=1 to 5;
y_i.=i;
end;
run;


I want to keep the variables y_1, y_2 and so on (not i)

thanks in advance,
Sachin
Occasional Contributor
Posts: 8

Re: do loops and name appending

Posted in reply to SachinRuk
You could initialize an array with the name of the variables that you want.

For example, you could have said array y_(5); Then you reference the array like so in the data step: y_(i) = value;. When the array is stored in the dataset, it will actually be stored as y_1, y_2, etc.

Note; In the array statement, the number in parentheses tells sas how many elements you need in your array; for you example, it should match the number of iterations in the do loop.
SAS Super FREQ
Posts: 8,868

Re: do loops and name appending

And, this is a very good introduction to ARRAY processing and ARRAY concepts in SAS:
http://support.sas.com/rnd/papers/sgf07/arrays1780.pdf

cynthia
Super Contributor
Posts: 673

Re: do loops and name appending

Posted in reply to SachinRuk
Try this:




%macro sachin;
data newtable;
%do i=1 %to 5;
%let j=&i.;
y_&j.=&j.;
%end;
run;
%mend sachin;
%sachin;
Super Contributor
Posts: 673

Re: do loops and name appending

Posted in reply to SachinRuk
Applying Rambo's suggestion:


data newtable1;
array y_(5);
do i= 1 to 5;
y_(i)=i;
end;
drop i;
run;
Ask a Question
Discussion stats
  • 4 replies
  • 121 views
  • 0 likes
  • 4 in conversation