SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Adir92
Obsidian | Level 7

Hello to all, hope you are doing well.
I have a database TEST that has 5 columns. Here is an example:

 

ID_CustomerID_ProjectID_ApplicationNbre_ApplicationCreation_Application
2002001000267120JANV2020
2002001000256110JANV2020
2002001000456115JANV2020
4004002000120210FEB2020
4004002000101105JANV2020

 

For each client, I would like to classify his applications, from the oldest to the most recent with the number 1 for the oldest, 2 for the most recent...

Like this : 

 

ID_CustomerID_ProjectID_ApplicationNbre_ApplicationCreation_Application
200200100025611
200200100045612
200200100026713
400400200010111
400400200012022

 

 

I know there's a proc rank procedure but here how can I use it ? And is there a way with proc sql ? 

Thank you !

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

That would not be a "rank" , that would be a sequence number and not ideal for Proc SQL.

 

The question might be what happens if a single ID_customer value has multiple Id_project values and how that gets treated. The example below resets the sequence for each project. If that is not desired use First.id_customer

Proc sort data=test;
   by Id_customer id_project creation_application;
run;

data want;
   set test;
   by id_customer id_project;
   retain sequence;
   if first.id_project then sequence=1;
   else sequence+1;
run;

The BY statement creates boolean (true/false or 1/0) values for each variable that on the statement indicating whether the specific record is the first or last of a value group. These are accessed using the First. (see the dot) and Last. notation preceding the variable name.

Retain creates a variable whose value is kept across iteration boundaries of a data step so can be used to do sequences. But you need to reset it to a starting value when needed.

Bad juju results from changing the values of variables like Creation_application from a date to numeric, or was that just an accident in your "want"?

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26
proc rank data=have out=want;
    by id_customer;
    var creation_application;
    ranks ranked_creation_application;
run;
proc sort data=want;
     by id_customer ranked_creation_application;
run;

Or alternatively

 

proc sort data=have;
    by id_customer creation_application;
run;
data want;
    set have;
    by id_customer;
    if first.id_customer then ranked_creation_application=0;
    ranked_creation_application+1;
run;

Really no need for SQL here.

--
Paige Miller
Adir92
Obsidian | Level 7
Thank you !
ballardw
Super User

That would not be a "rank" , that would be a sequence number and not ideal for Proc SQL.

 

The question might be what happens if a single ID_customer value has multiple Id_project values and how that gets treated. The example below resets the sequence for each project. If that is not desired use First.id_customer

Proc sort data=test;
   by Id_customer id_project creation_application;
run;

data want;
   set test;
   by id_customer id_project;
   retain sequence;
   if first.id_project then sequence=1;
   else sequence+1;
run;

The BY statement creates boolean (true/false or 1/0) values for each variable that on the statement indicating whether the specific record is the first or last of a value group. These are accessed using the First. (see the dot) and Last. notation preceding the variable name.

Retain creates a variable whose value is kept across iteration boundaries of a data step so can be used to do sequences. But you need to reset it to a starting value when needed.

Bad juju results from changing the values of variables like Creation_application from a date to numeric, or was that just an accident in your "want"?

Adir92
Obsidian | Level 7
Yes I see, no It was an accident, its better with a created variable. Thank you so much ! I will read in detail your explanation to understand it well but the important thing is that it works!

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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