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?
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);
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;
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.
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.