BookmarkSubscribeRSS Feed
molla
Fluorite | Level 6
data table1;
input id visits .;
.;
datalines;
1 visit1
1 vsist2
1 unscheduledvisit
1 visit3
1 unscheduledvisit
2 visit1
2 vsist2
2 unscheduledvisit
2 visit3
2 unscheduledvisit

;
Run

Output data:
data table2
input id visits visit_no;
.;
datalines;
1 visit1. 1
1 vsist2. 2
1 unscheduledvisit. 2.1
1 visit3. 3
1 unscheduledvisit 3.1
2 visit1 1
2 vsist2 2
2 unscheduledvisit 2.1
2 visit3. 3
2 unscheduledvisit 3.1

;
Run


For every unscheduled visit for everyid the visitno should be incremented by .1 of previous visit,this shud be done automatically ,every thing shud be macroticed .kindly help me..
2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

You don't need a macro to do this.

 

See if this solves your problem

 

data table1;
input id visits $20.;
datalines;
1 visit1
1 vsist2
1 unscheduledvisit
1 visit3
1 unscheduledvisit
2 visit1
2 vsist2
2 unscheduledvisit
2 visit3
2 unscheduledvisit
;

data want;
   set table1;
   by id;
   if first.id then visit_no = 0;
   if visits = 'unscheduledvisit' then visit_no + .1;
   else visit_no = int(visit_no) + 1;
run;

 

Result

 

id  visits            visit_no 
1   visit1            1.0 
1   vsist2            2.0 
1   unscheduledvisit  2.1 
1   visit3            3.0 
1   unscheduledvisit  3.1 
2   visit1            1.0 
2   vsist2            2.0 
2   unscheduledvisit  2.1 
2   visit3            3.0 
2   unscheduledvisit  3.1 
FreelanceReinh
Jade | Level 19

I'd recommend involving the ROUND function in the incrementation (even though this might require a RETAIN statement). Otherwise the non-integer visit numbers are prone to rounding errors as soon as there are two or more unscheduled visits in a row. For example, 1+.1+.1 ne 1.2 (in SAS under Windows).

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 552 views
  • 1 like
  • 3 in conversation