01-31-2017 09:52 AM - edited 01-31-2017 09:53 AM
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?
01-31-2017 10:01 AM
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.));
visit = catx(' ','UNSCHEDULED',i);
01-31-2017 10:07 AM
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;
01-31-2017 10:50 AM - edited 01-31-2017 11:42 AM
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.
01-31-2017 12:01 PM
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;