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

Hello,

 

I'm trying to create a visit number variable for a longitudinal data set that is formatted long. My data set has an ID and a visit date, for example:

 

IDVisit Date
69608/30/2016
110736/8/2016
110938/23/2016
111299/28/2017
112744/13/2016
1127410/10/2016
112745/8/2017
1128811/30/2017
113159/25/2017
113152/27/2018
113158/21/2018
113228/8/2017
114531/27/2017
114538/15/2017
114531/29/2018
114537/17/2018
115334/14/2016
115339/7/2017
115336/13/2018

 

I'd like to end up with this:

 

IDVisit Datevisit
69608/30/20161
110736/8/20161
110938/23/20161
111299/28/20171
112744/13/20161
1127410/10/20162
112745/8/20173
1128811/30/20171
113159/25/20171
113152/27/20182
113158/21/20183
113228/8/20171
114531/27/20171
114538/15/20172
114531/29/20183
114537/17/20184
115334/14/20161
115339/7/20172
115336/13/20183

 

I used this code:

 

proc sort data = dataset;
by ID Visit Date;
run;

 

data dataset2;
set dataset;

by ID;
visit+1;
if first.ID then visit = 1;

run;

 

I ended up with this:

 

IDVisit Datevisit
69608/30/20161
110736/8/20162
110938/23/20163
111299/28/20171
112744/13/20162
1127410/10/20163
112745/8/20174
1128811/30/20175
113159/25/20171
113152/27/20182
113158/21/20183
113228/8/20174
114531/27/20171
114538/15/20172
114531/29/20183
114537/17/20184
115334/14/20165
115339/7/20176
115336/13/20187

 

Any advise would be very helpful.

 

Thank you very much.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Try

proc sort data = dataset;
by ID Visit Date;
run;

 

data dataset2;
set dataset;

by ID;
if first.ID then visit = 1;

else visit+1;

run;

 

View solution in original post

5 REPLIES 5
novinosrin
Tourmaline | Level 20

Try

proc sort data = dataset;
by ID Visit Date;
run;

 

data dataset2;
set dataset;

by ID;
if first.ID then visit = 1;

else visit+1;

run;

 

mt88
Calcite | Level 5

Thank you for your response. I adjusted the code as you suggested, but I got the same result.

novinosrin
Tourmaline | Level 20

Hi @mt88   Here is a test using the sample you posted

 



data have;
input ID	VisitDate :mmddyy10.;
format visitdate mmddyy10.;
cards;
6960	8/30/2016
11073	6/8/2016
11093	8/23/2016
11129	9/28/2017
11274	4/13/2016
11274	10/10/2016
11274	5/8/2017
11288	11/30/2017
11315	9/25/2017
11315	2/27/2018
11315	8/21/2018
11322	8/8/2017
11453	1/27/2017
11453	8/15/2017
11453	1/29/2018
11453	7/17/2018
11533	4/14/2016
11533	9/7/2017
11533	6/13/2018
;

data want;
set have;
by id;
if first.id then visit=1;
else visit+1;
run;

proc print noobs;run;
ID VisitDate visit
6960 08/30/2016 1
11073 06/08/2016 1
11093 08/23/2016 1
11129 09/28/2017 1
11274 04/13/2016 1
11274 10/10/2016 2
11274 05/08/2017 3
11288 11/30/2017 1
11315 09/25/2017 1
11315 02/27/2018 2
11315 08/21/2018 3
11322 08/08/2017 1
11453 01/27/2017 1
11453 08/15/2017 2
11453 01/29/2018 3
11453 07/17/2018 4
11533 04/14/2016 1
11533 09/07/2017 2
11533 06/13/2018 3
cminard
Quartz | Level 8

data temp;
input ID VisitDate:mmddyy10.;
format VisitDate mmddyy10.;
datalines;
6960 8/30/2016
11073 6/8/2016
11093 8/23/2016
11129 9/28/2017
11274 4/13/2016
11274 10/10/2016
11274 5/8/2017
11288 11/30/2017
11315 9/25/2017
11315 2/27/2018
11315 8/21/2018
11322 8/8/2017
11453 1/27/2017
11453 8/15/2017
11453 1/29/2018
11453 7/17/2018
11533 4/14/2016
11533 9/7/2017
11533 6/13/2018
;
run;

proc sort data=temp; by id visitdate; run;
data temp2;
set temp;
by id;
if first.id then VisitNumber=1;
else VisitNumber+1;
run;

Astounding
PROC Star
This is not a valid name for a variable:

Visit date

So the program you are presenting is not plausible unless ....

A likely cause of the results you describe: perhaps your original data set already contains a variable named VISIT. In that case, the results will be thrown off because the program modifies an existing variable instead of creating a new variable.