DATA Step, Macro, Functions and more

rename unscheduled visit sequentially

Reply
Super Contributor
Posts: 312

rename unscheduled visit sequentially

[ Edited ]

in my data, for the same person with unscheduled visits, they are all coded as 99.1 (visit number) and "Unscheduled 1" (visit name). 

I want to recode the second one as 99.2 and 'Unscheduled 2', the third one as 99.2 and 'Unscheduled 3', etc. 

data unvisit2 ;
set unvisit1;
by usubjid visit vsdt;
i=1; if first.visit then i=1; else do; i=i+1; visitnum=visitnum+0.1; visit='UNSCHEDULED ' || strip(put(i,best.)); end;
drop i; run;

 

Is there a better way to do it? 

Super User
Posts: 10,538

Re: rename unscheduled visit sequentially

If your VSDT variable is a date then I don't see much that could be done.

I might be concerned that you are setting all visits to Unscheduled except the first one, but you know your data.

 

I would probably replace visit='UNSCHEDULED ' || strip(put(i,best.));

with

visit = catx(' ','UNSCHEDULED',i);

 

Super Contributor
Posts: 312

Re: rename unscheduled visit sequentially

Thanks for the CAT function.
Super User
Super User
Posts: 7,413

Re: rename unscheduled visit sequentially

Well, does your code work?  i is not retained across rows, so it will aways be . when you start.  AS you haven't provided any test data (in the form of a datastep) I can't give working code, but something like:

data want;
  set have;
  retain visitnum;
  if visit="Unscheduled" then visitnum=visitnum+0.1;
  visit=ifc(first.visit,"Unscheduled 1",catx(" ","Unscheduled",put(visitnum,best.)));
run;
  
Super Contributor
Posts: 312

Re: rename unscheduled visit sequentially

[ Edited ]

Thank you for your comments. My code has a problem, that the third one is also coded 99.2.

Without the i=1; statement before the if statement, i will be missing for non-first ones.

 

Super User
Super User
Posts: 7,413

Re: rename unscheduled visit sequentially

I did not even see this, this is a good example of why badly formatted code is hard to read.  Try:

data unvisit2 (drop=i);
  set unvisit1;
  by usubjid visit vsdt;
  i=1;
  if not(first.visit) then do;
    i=i+1;
    visitnum=visitnum+0.1;
    visit=catx(" ",'UNSCHEDULED',put(i,best.));
  end;
run;
Ask a Question
Discussion stats
  • 5 replies
  • 119 views
  • 0 likes
  • 3 in conversation