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

I am trying to use a do loop to number each individual issue date by account number in my data set.  Here is my data set:

 

AccountNumber     IssueDate      IssueNumber

        1001                1/1/2020               

        1001                2/1/2020               

        1001                3/1/2020               

        1001                4/1/2020               

        1002                1/2/2020               

        1002                1/15/2020             

        1003                1/5/2020               

        1003                1/20/2020             

        1003                1/31/2020             

 

I want to populate the IssueNumber field with the iterative do loop to number each entry for every individual AccountNumber as you see in .  The AccountNumber could have 1, 2, 5, 10, up to 52.

 

    DATA Records;

    SET Master;
DO i = 1 to 52; /*value range - could be less according to count of issuedate field*/
    DO IssueNumber = 1 to i;
        output;
            END;
    END;
RUN;

This is how I want to see from the output data set. 

 

AccountNumber     IssueDate      IssueNumber

        1001                1/1/2020               1

        1001                2/1/2020               2

        1001                3/1/2020               3

        1001                4/1/2020               4

        1002                1/2/2020               1

        1002                1/15/2020             2

        1003                1/5/2020               1

        1003                1/20/2020             2

        1003                1/31/2020             3

 

 

 

The code I have provided above is not providing the output I need for analysis.  Any feedback would be greatly appreciated.  Thanks.

 

1 ACCEPTED SOLUTION

Accepted Solutions
RichardDeVen
Barite | Level 11

Use BY group processing and automatic variable FIRST.<byvar> to reset the counter at the start of each group.

 

Example:

data want;
  set have;
  by AccountNumber;
  if first.AccountNumber
then IssueNumber = 1;
else IssueNumber + 1; /* sum statement */ run;

View solution in original post

2 REPLIES 2
RichardDeVen
Barite | Level 11

Use BY group processing and automatic variable FIRST.<byvar> to reset the counter at the start of each group.

 

Example:

data want;
  set have;
  by AccountNumber;
  if first.AccountNumber
then IssueNumber = 1;
else IssueNumber + 1; /* sum statement */ run;
greg6363
Obsidian | Level 7
It worked. Thanks so much. Appreciate it.

SAS Innovate 2025: Call for Content

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 518 views
  • 2 likes
  • 2 in conversation