I asked this question on RunSubmit too, since SAS Q&A community seems a bit scattered. If this is not appreciated, please let me know.
Is it possible in a data step to return to a previous/certain observation and go from there further through the data set?
To add to the question in case it is still not clear, a small example:
Data set 'work.test' :
name | number
John | 1
Jack | 2
Jane | 3
Jade | 4
Ronn | 5
Dick | 6
Sofy | 7
Sady | 8
Ruth | 9
Data step:
Data _null_;
set work.test;
File ...\test.txt;
put name;
if number = 5 and counter=3 then do;
counter = counter+1;
*return to obs where number = 3* AKA *set obs pointer back to obs with nr=3*;
end;
run;
*The question is about the part return to obs where number = 1 , not about [and first time] , that is only added so it would not generate and endless loop.*
Additional info:
As it seems to still not be 100% clear what I want to do, I added a bit to the original sample data set and example.
Just remember it should be a bit generic and not be fixed code. Conditions might vary later on. But the main question is just: "is it possible to go back to obs=X and go from there when you are at obs=Y and how?"
Background info: This fits into the whole story of creating an xml output using a single table holding the xml flow, where some elements need to be repeated. No, it is not possible using XML map because of old SAS version. No, ODS is also not applicable in this case. Btw, this is just background info because the remark 'I still don't knwo what you try to do' keeps coming up ;)
List of possibilities NOT applicable for my needs:
- REWIND function (returns only to the first observation)
- LAG function (to get previous value of variable, not for going back in obs)
- Using multiple SET statements: this is not generic in the way I need it (ie. some data sets would require 2 loops, others would require 5 loops, thus 5 nested SET statements...)
What COULD work:
- Use of complete SCL code to go through data step (postponed)
- POINT option in the SET statement (current try out)
- Hash tables: still have to do more research as I do not know much about this or how to implement.