DATA Step, Macro, Functions and more

Do while loop

Accepted Solution Solved
Reply
Contributor
Posts: 34
Accepted Solution

Do while loop

good day I have a data set below that has user_no (there could be multiple user use_no with different dates)

I want to create a loop that adds a maximum date unitl 201802 for eg:

 

if the last date for user 123 is 201711 I want the loop the add 201712 / 201801 / 201802

 

the data looks below :  I have not attempted the code :

 

user_no month New_month
123 201505 201505
123 201506 201506
123 201507 201507
123 201508 201508
123 201509 201509
123 201510 201510
123 201511 201511
123 201512 201512
123 201601 201601
123 201602 201602
123 201603 201603
123 201604 201604
123 201605 201605
123 201606 201606
123 201607 201607
123   201608
123   201609
123   201610
123    ""
123    ""
123    ""
123    ""
123    "'
123   201802

Accepted Solutions
Solution
‎02-22-2018 02:29 AM
Super User
Posts: 9,593

Re: Do while loop

Posted in reply to Allistair

Do by-group-processing and add this in the data step:

output;
if last.user
then do while (new_month < '201801');
  new_month = /* next month to be calculated according to the values; with SAS dates, use the intnx() function */;
  output;
end;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
‎02-22-2018 02:29 AM
Super User
Posts: 9,593

Re: Do while loop

Posted in reply to Allistair

Do by-group-processing and add this in the data step:

output;
if last.user
then do while (new_month < '201801');
  new_month = /* next month to be calculated according to the values; with SAS dates, use the intnx() function */;
  output;
end;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 34

Re: Do while loop

Posted in reply to KurtBremser
thank you.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 98 views
  • 0 likes
  • 2 in conversation