BookmarkSubscribeRSS Feed
nanavel
Calcite | Level 5

I have longitudinal data. Each participant has multiple data entries for each visit.
Unfortunately, the data does not have unique visit numbers for each participant.
For eg. Id 1 will have visit numbers 111,484,222,222, 456 
And ID 2 will have to visit no’s 890, 098, 098, 450.
I would like to write a code that can assign
e.g., Id 1 visits as 111=1 ,484=2,222=3,222=3, 456=4
And I’d 2 will have visits as 890=1, 098=2, 098=2, 450=3.

This will provide consistency for all study participants for further analysis.

Any guidance will be appreciated. Thank you.

3 REPLIES 3
Reeza
Super User

Add an enumeration/counter variable.

 

https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/

 


@nanavel wrote:

I have longitudinal data. Each participant has multiple data entries for each visit.
Unfortunately, the data does not have unique visit numbers for each participant.
For eg. Id 1 will have visit numbers 111,484,222,222, 456 
And ID 2 will have to visit no’s 890, 098, 098, 450.
I would like to write a code that can assign
e.g., Id 1 visits as 111=1 ,484=2,222=3,222=3, 456=4
And I’d 2 will have visits as 890=1, 098=2, 098=2, 450=3.

This will provide consistency for all study participants for further analysis.

Any guidance will be appreciated. Thank you.


 

nanavel
Calcite | Level 5

Thank you Reeza. When I try adding an enumeration, I get it as counting variables for each datapoint per participant. For e.g. ID 1 will be numbered 1-20. Meanwhile, some of them will be same-day visits so I would want those as repeats but it does not work with this code.

 

proc sort data=-data have;

by id;

run;

data want;

set have;

count + 1;

by id;

if first.id then count=1;

run;

Reeza
Super User
Try adding the DATE to your BY statement and using FIRST.DATE instead of FIRST.ID.

There are more complicated examples in that post that illustrate how to do it within different By groupings.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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