-1

I am trying to insert new rows for each id, the number of rows should be equal to the value of a certain variable.

For example, say I have the variables ID and X1 which look like this:

ID      X1
A       3
B       1
C       5

Then I want the table to be produced as this:

ID
A
A
A
B
C
C
C
C
C

So there are 3 rows for A, 1 for B, and 5 for C. Thanks.

  • Why? What did you try? How close did it come? – Tom Oct 12 '18 at 23:36
  • The X1 variable represents how many occurrences are remaining for each ID. To perform a forecast for each upcoming period I need to have each ID repeat for as many remaining periods that are left. I've tried some macro work but I am very limited in skill – user10497521 Oct 13 '18 at 01:39
  • This does not require a macro, it's a basic DO loop with an OUTPUT statement. – Reeza Oct 13 '18 at 02:08
  • First one is a freebie, but please post an attempt next time. – Reeza Oct 13 '18 at 02:12

1 Answers1

1

Use a DO loop with an explicit OUTPUT statement to duplicate the records X1 times.

data want;
     set have;
     do i=1 to x1;
        output;
     end;
   run;
Reeza
  • 20,510
  • 4
  • 21
  • 38