Help using Base SAS procedures

Do Loop over the same observation

Reply
Occasional Contributor
Posts: 9

Do Loop over the same observation

Hello!

I'm trying to make this bit of code more robust:

if 99 le days le 182 then  visit='6 month';
else if days le ceil(((9*30.5)+14)) then visit='9 month';
else if days le ceil(((12*30.5)+14)) then visit='12 month';
else if days le ceil(((15*30.5)+14)) then visit='15 month';
else if days le ceil(((18*30.5)+14)) then visit='18 month';
else if days le ceil(((21*30.5)+14)) then visit='21 month';
else if days le ceil(((24*30.5)+14)) then visit='24 month';
else if days le ceil(((27*30.5)+14)) then visit='27 month';
else if days le ceil(((30*30.5)+14)) then visit='30 month';
else if days le ceil(((33*30.5)+14)) then visit='33 month';
else if days le ceil(((36*30.5)+14)) then visit='36 month';
else if days le ceil(((39*30.5)+14)) then visit='39 month';
else if days le ceil(((42*30.5)+14)) then visit='42 month';
else if days le ceil(((45*30.5)+14)) then visit='45 month';

And have been able to somewhat make it more robust using this method:

do i=9 by 3 until(last.id);

  set origvisits;

  days = VISITDTC -trtstdt;

  label days='Number of days since original ib1001 start';

  if 99 le days le 182 then  visit='6 month';

  by id ;

  %let n=i;

  if days le ceil(((i*30.5)+14)) then visit=strip(&n||' month');

  else if days le ceil((((3+i)*30.5)+14)) then visit=strip(&n+3||' month');

  else if days le ceil((((6+i)*30.5)+14)) then visit=strip(&n+6||' month');

  else if days le ceil((((9+i)*30.5)+14)) then visit=strip(&n+9||' month');

  else if days le ceil((((12+i)*30.5)+14)) then visit=strip(&n+12||' month');

  output;

end;

But its still is not robust enough.

What I've constructed above is each observation is being read in with i=9 and increasing by 3 until last.id. And then i increments sequentially for each observation within that by group (id, in this case)


What I really want SAS to do is read one observation at a time and keep trying values of i (the index of the do loop) until the condition if days le ceil(((i*30.5)+14))  is true, so that I can assign a nominal visit date.


For example, suppose the condition  days le (9*30.5)+14 is not true, I want SAS to stay with that same observation and try (12*30.5)+14, if that condition is not true then try (15*30.5)+14 until the days le (i*30.5)+14 condition is true. Then move on to the next observation in the dataset, with i being reset back to 9.


I hope this makes sense, I know what I want SAS to do...but can't quite articulate it.


Any help or guidance would be appreciated

Contributor dkb
Contributor
Posts: 53

Re: Do Loop over the same observation

The first two groups (6 and 9 months) don't follow the general rule so they have to be coded separately.  The others follow a simple rule based on the number of days; this is one way of many to approach it:

if 99 le days le 182 then  visit=' 6 month';

else if days le ceil(((9*30.5)+14)) then visit=' 9 month';

else do;

   nmon = 3 * ceil((days-14.5) / (3 * 30.5));

   if nmon le 99 then visit = put(nmon, 2.)!! ' month';

  else put 'ERROR: Danger, Will Robinson! Numeric field overflow!';

end;

Super User
Posts: 17,907

Re: Do Loop over the same observation

Contributor dkb
Contributor
Posts: 53

Re: Do Loop over the same observation

Thanks Reeza. I hadn't realised the OP was spraying.

Ask a Question
Discussion stats
  • 3 replies
  • 238 views
  • 1 like
  • 3 in conversation