Essentially I need to repeat rows of data till I reach a specific number of rows. For example, say I have 5 rows of data I need to fill 12 rows. That is, I have:
| Index | ID |
:-------:----:
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | D |
| 5 | E |
But I need:
| Index | ID |
:-------:-----
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | D |
| 5 | E |
| 6 | A |
| 7 | B |
| 8 | C |
| 9 | D |
| 10 | E |
| 11 | A |
| 12 | B |
This is going to be on a much larger scale. The index does not have to be included. I can think of how to do this in R or Python, but I have very minimal experience in SAS.
What is the source of data you want use?
What is the source of the number of rows you want to create?
Why the heck would you ever want to do this?
Anyway it seems simple enough. Let's create the source dataset.
data have;
input id $ @@;
cards;
A B C D E
;
Now let's keep re-reading it until we get to 12 observations.
data want;
if _n_>12 then stop;
point=1+mod(_n_-1,nobs);
set have point=point nobs=nobs;
run;
Results:
Obs id 1 A 2 B 3 C 4 D 5 E 6 A 7 B 8 C 9 D 10 E 11 A 12 B
@Transcendental wrote:
Honestly if I were doing this in R I would be do something completely different for the task at hand because R has better capabilities for data structures. But if I just needed to do something like this, I would use a for loop. I know there are DO loops in SAS but I would take advantage of easy indexing in R: I could say table_name[2, 4] and it would give me the contents of the cell in the second row and fourth column. So in a loop I could do something like table_name[i, 3] = .... to add to the third column.
As far as I'm aware there's no equivalency of that in SAS.
Are you are asking the wrong question?
data _12;
if _n_ gt 12 then stop;
set have have have;
run;
What is the source of data you want use?
What is the source of the number of rows you want to create?
Why the heck would you ever want to do this?
Anyway it seems simple enough. Let's create the source dataset.
data have;
input id $ @@;
cards;
A B C D E
;
Now let's keep re-reading it until we get to 12 observations.
data want;
if _n_>12 then stop;
point=1+mod(_n_-1,nobs);
set have point=point nobs=nobs;
run;
Results:
Obs id 1 A 2 B 3 C 4 D 5 E 6 A 7 B 8 C 9 D 10 E 11 A 12 B
@Transcendental wrote:
Essentially, I'm creating a snake draft, but there's going to be roughly 50+ users (via usernames in one table), changing weekly. And the source of the number of rows (players that can be chosen, in a different table) are also changing weekly.
This is very helpful! Thank you!
That says TWO tables (at least) are involved. Which is quite different conceptually than what you show in the original question.
So why do you ask about adding a fixed number of rows? Perhaps the data with a proper join between the two tables takes care of things.
Or look at Proc IML
Do you just want to generate that dataset from a program?
data want ;
length index 8 id $1 ;
index=0;
do until(index>=12);
do id='A','B','C','D','E';
index+1;
output;
if index >=12 then leave;
end;
end;
run;
As @ballardw said, you should consider SAS/IML . It is very like R language.
data have;
input id $ @@;
cards;
A B C D E
;
proc iml;
use have;
read all var _all_ into x[c=vname];
close;
want=shape(x,12,ncol(x));
print want[c=vname l=''];
quit;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: