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

hackathon24-white-horiz.png

Join the 2025 SAS Hackathon!

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!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 931 views
  • 1 like
  • 3 in conversation