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

Hello all!

 

I have a question about how to assign a sequential number to each subject based on another variable that is a date.

 

This would be the start dataset:

id     date

1     1/10/07

1     3/23/07

1     5/23/08

2     2/10/07

2     2/01/08

3     8/01/12

3     1/23/13

3     2/10/14

3     7/10/15

 

This is what I want as the end result:

id     date          seq

1     1/10/07      1

1     3/23/07      2

1     5/23/08      3

2     2/10/07      1

2     2/01/08      2

3     8/01/12      1

3     1/23/13      2

3     2/10/14      3

3     7/10/15      4

 

Any advice?

1 ACCEPTED SOLUTION

Accepted Solutions
rajeshalwayswel
Pyrite | Level 9
data a;
infile datalines missover;
input id  date $10. ;
datalines;
1 1/10/07
1 3/23/07
1 5/23/08
2 2/10/07
2 2/01/08
3 8/01/12
3 1/23/13
3 2/10/14
3 7/10/15
run;

proc sort data=a out=a1;by id;run;

data a2;
set a1;
by id;
if first.id then seq=1 ;
else seq+1;
run;

View solution in original post

3 REPLIES 3
rajeshalwayswel
Pyrite | Level 9
data a;
infile datalines missover;
input id  date $10. ;
datalines;
1 1/10/07
1 3/23/07
1 5/23/08
2 2/10/07
2 2/01/08
3 8/01/12
3 1/23/13
3 2/10/14
3 7/10/15
run;

proc sort data=a out=a1;by id;run;

data a2;
set a1;
by id;
if first.id then seq=1 ;
else seq+1;
run;
SarahW13
Obsidian | Level 7

Thank you so much! It worked!

 

The only thing that I changed is that when I did proc sort, I did this:

proc sort data=dataset;

by id date;

run;

 

Not sure if it would have still worked with only sorting by id...but I sorted by both id and date since I wanted the Seq to be in the order of the dates.

 

Thanks again!

ballardw
Super User

@SarahW13 wrote:

Thank you so much! It worked!

 

The only thing that I changed is that when I did proc sort, I did this:

proc sort data=dataset;

by id date;

run;

 

Not sure if it would have still worked with only sorting by id...but I sorted by both id and date since I wanted the Seq to be in the order of the dates.

 

Thanks again!


You inclusion of date is correct.

If you change the order of the data as read, switched the 2nd and 3rd rows and sort by Id the values are not in the desired order:

data a;
infile datalines missover;
input id  date $10. ;
datalines;
1 1/10/07
1 5/23/08
1 3/23/07
2 2/10/07
2 2/01/08
3 8/01/12
3 1/23/13
3 2/10/14
3 7/10/15
run;

proc sort data=a; 
by id;
run;

But I strongly suggest using a DATE value instead of character as otherwise your data will not sort correctly with the date:

 

Please see:

data a;
infile datalines missover;
input id  date $10. ;
datalines;
1 1/10/07
1 5/23/08
1 3/23/07
1 11/10/04
2 2/10/07
2 2/01/08
3 8/01/12
3 1/23/13
3 2/10/14
3 7/10/15
run;

proc sort data=a; 
by id date;
run;

Note that 11/10/04 comes after 1/10/07. Character sort goes character by character left to right.

 

So something more like this is likely actually what you want:

data a;
infile datalines missover;
input id  date mmddyy8. ;
format date mmddyy8.;
datalines;
1 1/10/07
1 5/23/08
1 3/23/07
1 11/10/04
2 2/10/07
2 2/01/08
3 8/01/12
3 1/23/13
3 2/10/14
3 7/10/15
run;

proc sort data=a; 
by id date;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 3377 views
  • 1 like
  • 3 in conversation