Help using Base SAS procedures

How to Number Visit Dates per ID.

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

How to Number Visit Dates per ID.

Hi guys,

 

Need some help while flagging visits per subject id.

 

Example data :

 

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;

 

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 Smiley Happy

 

 


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;
 set task1;
 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;

View solution in original post


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;
 set task1;
 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 Smiley Happy 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
 from task1
  group by pt;
quit;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 298 views
  • 3 likes
  • 2 in conversation