DATA Step, Macro, Functions and more

Best way to repeat the same steps over and over until a value is reached?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 128
Accepted Solution

Best way to repeat the same steps over and over until a value is reached?

Hi,

 

I have four-ish SAS steps that i want to repeat until no more records are deleted in the last step.


I was thinking do/until sounded right, but i have never used it before, so i wasn't sure.  So keep in mind, while i'm playing around with writing stuff, nothing is currently running.  This is pretty much abstract theory so far.

 

But i was thinking:

 

%macro

%do %until (var=0);

  blah - frequency

  blah - second separate data step

  blah - third separate data step

  blah - other data step

  blah - check dictionary tables for record counts

  var=[record counts]-[record counts]

%end;

%mend;

 

So in step 5, it was grabbing the nobs from the dictionary tables, so could tell whether any records were deleted in step 4, and have a tangible count of it.  If no records were deleted, stop processing, if records were deleted, process the cycle again.

 

But then i realized i needed an iterative counter to track the datasets through each cycle and then i realized i had to write in a split path at the top so that first time through it would take one dataset to start, but second+ it would take the end table from the previous time through, and now i'm wondering if this is even the best way to do it.

 

What do you guys think?

 

Thanks.


Accepted Solutions
Solution
‎09-23-2016 09:29 AM
Frequent Contributor
Posts: 128

Re: Best way to repeat the same steps over and over until a value is reached?

Posted in reply to Astounding

I did end up getting this to work with a do/until and built in counter, but the code is too long to post here.  If someone wants to send me a message with a question, feel free.  But i don't know how to delete posts, so i'm going to use this one as the solution. Smiley Happy

View solution in original post


All Replies
Super User
Posts: 5,503

Re: Best way to repeat the same steps over and over until a value is reached?

Since the question is conceptual only at this point, here are some gut reactions.

 

First, since the possibility of an infinite loop exists, I would be very careful to make sure the program ends (regardless of the final coding techniques used).

 

Second, I would reuse the same data set name until SAS is sick of it.  For example, capture the number of records after step 3 into a macro variable &BEFORE, then capture the number of records after step 4 into &AFTER.  See if &BEFORE=&AFTER.  Then you don't need to count number of iterations, and you don't need to change the data set names each time.

Solution
‎09-23-2016 09:29 AM
Frequent Contributor
Posts: 128

Re: Best way to repeat the same steps over and over until a value is reached?

Posted in reply to Astounding

I did end up getting this to work with a do/until and built in counter, but the code is too long to post here.  If someone wants to send me a message with a question, feel free.  But i don't know how to delete posts, so i'm going to use this one as the solution. Smiley Happy

☑ This topic is solved.

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

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