Solved
New Contributor
Posts: 2

# How to Number Visit Dates per ID.

Hi guys,

Need some help while flagging visits per subject id.

Example data :

format pt best12. ht best12. wt best12. colldate \$10. sex \$8.;
input pt ht wt colldate \$10. sex \$8.;
cards;
1560401 155 70 22-Mar-06 Male
1560401 155 75 22-Jan-05 Male
1560402 150 60 03-Apr-05 Female
1560402 150 75 22-Jan-06 Female
1560403 165 70 22-Mar-06 Male
1560403 165 75 22-Jan-05 Male
1560404 150 60 03-Apr-05 Male
1560404 150 75 22-Jan-06 Male
1560405 145 50 22-Jan-06 Male
1560405 145 55 22-Jan-05 Male
1560406 150 80 03-Apr-05 Male
1560406 150 75 11-Apr-05 Male
1560407 165 70 15-Mar-06 Male
1560408 150 60 12-Dec-05 Female
1560408 150 75 22-Jan-06 Female
1560409 99 70 22-Oct-06 Male
1560409 99 75 22-May-05 Male
1560410 150 60 03-Dec-05 Male
1560410 150 105 22-Jan-06 Male
run;

I would like to create new variable called VISIT. VISIT should be = 10 for patients with first visit (earliest), VISIT = 20 for last visit (later) and VISIT = 30 if patient had only one visit. Looking forward for your help

Accepted Solutions
Solution
‎11-27-2017 10:22 AM
Super User
Posts: 10,761

## Re: How to Number Visit Dates per ID.

Whether it have at most two obs for each PT or not?

``````data task1;
format pt best12. ht best12. wt best12. colldate \$10. sex \$8.;
input pt ht wt colldate \$10. sex \$8.;
cards;
1560401 155 70 22-Mar-06 Male
1560401 155 75 22-Jan-05 Male
1560402 150 60 03-Apr-05 Female
1560402 150 75 22-Jan-06 Female
1560403 165 70 22-Mar-06 Male
1560403 165 75 22-Jan-05 Male
1560404 150 60 03-Apr-05 Male
1560404 150 75 22-Jan-06 Male
1560405 145 50 22-Jan-06 Male
1560405 145 55 22-Jan-05 Male
1560406 150 80 03-Apr-05 Male
1560406 150 75 11-Apr-05 Male
1560407 165 70 15-Mar-06 Male
1560408 150 60 12-Dec-05 Female
1560408 150 75 22-Jan-06 Female
1560409 99 70 22-Oct-06 Male
1560409 99 75 22-May-05 Male
1560410 150 60 03-Dec-05 Male
1560410 150 105 22-Jan-06 Male
;
run;
data want;
by pt;
if first.pt=1 and last.pt=0 then visit=10;
if first.pt=0 and last.pt=1 then visit=20;
if first.pt=1 and last.pt=1 then visit=30;
run;``````

All Replies
Solution
‎11-27-2017 10:22 AM
Super User
Posts: 10,761

## Re: How to Number Visit Dates per ID.

Whether it have at most two obs for each PT or not?

``````data task1;
format pt best12. ht best12. wt best12. colldate \$10. sex \$8.;
input pt ht wt colldate \$10. sex \$8.;
cards;
1560401 155 70 22-Mar-06 Male
1560401 155 75 22-Jan-05 Male
1560402 150 60 03-Apr-05 Female
1560402 150 75 22-Jan-06 Female
1560403 165 70 22-Mar-06 Male
1560403 165 75 22-Jan-05 Male
1560404 150 60 03-Apr-05 Male
1560404 150 75 22-Jan-06 Male
1560405 145 50 22-Jan-06 Male
1560405 145 55 22-Jan-05 Male
1560406 150 80 03-Apr-05 Male
1560406 150 75 11-Apr-05 Male
1560407 165 70 15-Mar-06 Male
1560408 150 60 12-Dec-05 Female
1560408 150 75 22-Jan-06 Female
1560409 99 70 22-Oct-06 Male
1560409 99 75 22-May-05 Male
1560410 150 60 03-Dec-05 Male
1560410 150 105 22-Jan-06 Male
;
run;
data want;
by pt;
if first.pt=1 and last.pt=0 then visit=10;
if first.pt=0 and last.pt=1 then visit=20;
if first.pt=1 and last.pt=1 then visit=30;
run;``````
New Contributor
Posts: 2

## Re: How to Number Visit Dates per ID.

Thanks a lot working perfectly ! Any idea how i can handle this using PROC SQL?

Super User
Posts: 10,761

## Re: How to Number Visit Dates per ID.

OK. No problem.

``````data task1;
format pt best12. ht best12. wt best12. colldate  date9. sex \$8.;
input pt ht wt colldate : date9. sex \$8.;
cards;
1560401 155 70 22-Mar-06 Male
1560401 155 75 22-Jan-05 Male
1560402 150 60 03-Apr-05 Female
1560402 150 75 22-Jan-06 Female
1560403 165 70 22-Mar-06 Male
1560403 165 75 22-Jan-05 Male
1560404 150 60 03-Apr-05 Male
1560404 150 75 22-Jan-06 Male
1560405 145 50 22-Jan-06 Male
1560405 145 55 22-Jan-05 Male
1560406 150 80 03-Apr-05 Male
1560406 150 75 11-Apr-05 Male
1560407 165 70 15-Mar-06 Male
1560408 150 60 12-Dec-05 Female
1560408 150 75 22-Jan-06 Female
1560409 99 70 22-Oct-06 Male
1560409 99 75 22-May-05 Male
1560410 150 60 03-Dec-05 Male
1560410 150 105 22-Jan-06 Male
;
run;
proc sql;
select *,case when(count(*)=1) then 30
when(colldate=min(colldate)) then 10
when(colldate=max(colldate)) then 20
end as visit