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
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).
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!
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.