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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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