BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
fabdu92
Obsidian | Level 7

Hi,

 

I have a dataset like this:

 

DataA DataB DataC

ajbg   34   31/01/2000

hytv   20   31/08/1995

ytffg   54   28/02/2005

...

 

I would like to duplicate rows for each row. How many times? Exactly the number of time of DataB: 34 times for the first row, 20 for the second one, 54 for the third, in the way we have something like that at the end.

 

DataA DataB DataC

ajbg   34   31/01/2000

ajbg   33   29/02/2000

ajbg   32   31/03/2000

ajbg   31   30/04/2000

ajbg   30   31/05/2000

...

hytv   20   31/08/1995

hytv   19   30/09/1995

hytv   18   31/10/1995

hytv   17   30/11/1995

hytv   16   31/12/1995

hytv   15   31/01/1996

...

ytffg   54   28/02/2005

ytffg   53   31/03/2005

ytffg   52   30/04/2005

ytffg   51   31/05/2005

ytffg   50   30/06/2005

...

 

Is there a way to do this? Thanks for help, I am only a beginner on SAS

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Assuming your dates for datac are actually SAS date values and not characters perhaps:


data want;
   set have ;
   do datab=datab to 1 by -1;
      output;
      datac = intnx('month',datac,-1,'e');
   end;
run;

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, do loops:

data want;
  set have;
  do i=0 to datab;
    datac=datac+i;
    output;
  end;
run;

This creates a loop going from 0 to your number.  The record is then output once for each of these loops, so the first time it will be datac+0 -> output, then datac+1 output etc.

ballardw
Super User

Assuming your dates for datac are actually SAS date values and not characters perhaps:


data want;
   set have ;
   do datab=datab to 1 by -1;
      output;
      datac = intnx('month',datac,-1,'e');
   end;
run;
FreelanceReinh
Jade | Level 19

To obtain ascending months, simply replace -1 with 1 in the third argument of the INTNX function used in ballardw's approach.

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

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 3 replies
  • 2155 views
  • 1 like
  • 4 in conversation