BookmarkSubscribeRSS Feed
fengyuwuzu
Pyrite | Level 9

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? 

5 REPLIES 5
ballardw
Super User

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);

 

fengyuwuzu
Pyrite | Level 9
Thanks for the CAT function.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
  
fengyuwuzu
Pyrite | Level 9

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.

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2111 views
  • 0 likes
  • 3 in conversation