BookmarkSubscribeRSS Feed
SachinRuk
Calcite | Level 5
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
4 REPLIES 4
Rambo
Calcite | Level 5
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.
Cynthia_sas
Diamond | Level 26
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
SASPhile
Quartz | Level 8
Try this:




%macro sachin;
data newtable;
%do i=1 %to 5;
%let j=&i.;
y_&j.=&j.;
%end;
run;
%mend sachin;
%sachin;
SASPhile
Quartz | Level 8
Applying Rambo's suggestion:


data newtable1;
array y_(5);
do i= 1 to 5;
y_(i)=i;
end;
drop i;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1500 views
  • 0 likes
  • 4 in conversation