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

Hello All,

I have to create a rank column based on 2 column values. The columns in the table are Id and datecounter. 

I need to rank based on groups, group by order is id then datecounter.

Below is the example of data have

Id datecounter
11-Jan-22
21-Jan-22
210-Jan-22
25-Feb-22
310-Jan-22
311-Jan-22
311-Jan-22
311-Jan-22
415-Feb-22
515-Feb-22

 

Data want 

Id datecounterRank
11-Jan-221
21-Jan-221
210-Jan-222
25-Feb-223
310-Jan-221
311-Jan-222
311-Jan-223
311-Jan-224
415-Feb-221
515-Feb-221
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Basic approach:

 

proc sort data=have;
   by id datecounter;
run;

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

Note: this assumes that your dates are actually SAS date values and not some character version.

The sort may not be needed if your data is in order.

Retain keeps the values of a variable across data step boundaries.

When you use a By group then SAS creates automatic variables that indicate whether the current observation is the first or last of a by group and are 1/0 (true/false) valued so can be used in If as shown. Note the dot following the keyword First, or Last, then the variable name without a space.

 

Personally I would call this an ORDER - sequence determined, not a Rank -size of value involved.

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26
proc rank data=have out=want;
     by id;
     var datecounter;
     ranks rank;
run;
--
Paige Miller
ballardw
Super User

Basic approach:

 

proc sort data=have;
   by id datecounter;
run;

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

Note: this assumes that your dates are actually SAS date values and not some character version.

The sort may not be needed if your data is in order.

Retain keeps the values of a variable across data step boundaries.

When you use a By group then SAS creates automatic variables that indicate whether the current observation is the first or last of a by group and are 1/0 (true/false) valued so can be used in If as shown. Note the dot following the keyword First, or Last, then the variable name without a space.

 

Personally I would call this an ORDER - sequence determined, not a Rank -size of value involved.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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