BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Pitvip
Calcite | Level 5

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 🙂

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

3 REPLIES 3
Ksharp
Super User

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;
Pitvip
Calcite | Level 5

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

Ksharp
Super User

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 1464 views
  • 4 likes
  • 2 in conversation