DATA Step, Macro, Functions and more

Loop within a group/ID

Reply
New Contributor
Posts: 3

Loop within a group/ID

Hello

I'm new to this forum and I hope somebody could help me with this problem. I want to check data within an (Person-)ID, check every single line from the first entry to the last and when all checks are done pass to the next Person-ID. I could try it with if first.id or if last.id but then only the first and the last entry are considered what doesn't solve my problem. A simple example is:

idrecord codestart dateend datereason of diminutionrecord year
1312009  2009
1312009  2010
1212009201112011
1212009201112012
1212009201112013
1212009201112014
1212009201112015
2     
2     
etc.     

 

The result should be (simplified):

if 'end date' is missing then output: first line is accepted

if 'end date' is missing then output: second line is accepted

if 'end date' not missing and 'end date' eq record year than output: third line accepted

and here full stop, the rest of the lines are not accepted anymore, stop the loop within the group and pass to the next person-id.

 

So, basic problem: how tell SAS to execute a job within a certain id/group and as soon as the job is finished pass to the next one.

 

I would appreciate your help, thank you.

Super User
Posts: 5,516

Re: Loop within a group/ID

Maybe I'm not getting the objective right.  It sounds like you're making an easy problem difficult.  Wouldn't this give you the result you are asking for?

 

data want;

set have;

if end_date=. or end_date = record_year;

run;

 

SAS is still processing every record.  But the processing deletes the records that you don't want.

 

Or are you trying for a different result?

New Contributor
Posts: 3

Re: Loop within a group/ID

[ Edited ]
Posted in reply to Astounding

Hello

 

Thank you for your quick response.

You are perfectly right, I didn't see the woods for the trees!

Indeed, I managed today to solve my problem by following your suggestion, I took only those records I need, the others were not taken into consideration so that I have now a solution.

 

Nevertheless, in a next step I will sitll have to resolve my problem I tried to explain yesterday akwardly. I will have to compare lines withing the same ID, for example if in line 1 the code of diminution is 8 (I will have 10 different codes) then ignore other lines according to certain conditions within the same ID, etc., sort of do conditions within the same ID until the last ID and when coditions finished the quit for the next ID. I am aware that my example is not the best, I will think about it this week-end but at least I managed to solve the first problem.

 

Thank you very much

Alecs

Super User
Posts: 10,041

Re: Loop within a group/ID

[ Edited ]

There are many scenarios you didn't clarify .

 

CODE NOT TESTED.

 

data want;

n=0;

 do until(last.id);

 set have;

 by id;

 n+1;

 if n=1 and end_date=. then first=1;

 if n=2 and end_date=. then second=1;

 if n=3 and end_date=record_year then third=1;

end;

 

n=0;

 do until(last.id);

 set have;

 by id;

 n+1;

 if n le 3 and first and second and third then output;

end;

 

run;

end;

New Contributor
Posts: 3

Re: Loop within a group/ID

Hello Xia Keshan

 

Thanks for your answer and suggestion.

 

Indeed as I explained it to the preceding super user I found the solution with his suggestion but in fact I was looking for a solution a you posted. So what I'm looking for is going into this direction you propose and I will try it this week-end.

 

Thank you very much

Alex

Ask a Question
Discussion stats
  • 4 replies
  • 267 views
  • 0 likes
  • 3 in conversation