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).
Calling all data scientists and open-source enthusiasts! Want to solve real problems that impact your company or the world? Register to hack by August 31st!
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.