BookmarkSubscribeRSS Feed
hwangnyc
Quartz | Level 8

I have longitudinal data that is in long form. The vast majority of my cases have 5 sessions. But there is a cases 8 sessions thus skewing things a bit and a case with 6. I’d like to be able to plot a timeline where the ending session is their last/final session. Here’s how I envision my data. Any suggestions or issues you think would occur with this would be greatly appreciated. Thanks!

ID

Session

Want

100

1

1

100

2

2

100

3

3

100

4

4

100

5

Last

101

1

1

101

2

2

101

3

3

101

4

4

101

5

Last

102

1

1

102

2

2

102

3

3

102

4

4

102

5

Last

103

1

1

103

2

2

103

3

3

103

4

4

103

5

5

103

6

6

103

7

7

103

8

Last

104

1

1

104

2

2

104

3

3

104

4

4

104

5

5

104

6

Last

 

 

 

1 REPLY 1
ballardw
Super User

This will subset the data if sorted by id session:

data want;
   set have;
   by  id session;
   if session in (1,2,3,4) or Last.id;
run;

If you really want to add a variable as shown something like:

 

data want;
   set have;
   by  id session;
   length want $ 4.;
   if session in (1,2,3,4) then Want=put(session,best4.);
   else if last.id then Want='Last';
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 1 reply
  • 897 views
  • 0 likes
  • 2 in conversation