Help using Base SAS procedures

data manipulation

Reply
Frequent Contributor
Frequent Contributor
Posts: 79

data manipulation

I have a reorder question

I have a cluster set say below

date cluster_id code
05/31/2000 1   A

05/31/2000  1  B

05/31/2000 3   C

05/31/2000 3   E

05/31/2000 5   F

05/31/2001   1 A

05/31/2001  2 B

05/31/2001 5  C

05/31/2001 7D

05/31/2001 7 E

05/31/2001 7 F

I would like to change the cluster id so that its numbered according to numeric order; everything else stays the same barring the cluster id

So for example the final set

05/31/2000  1 A

05/31/2000  1 B

05/31/2000 2 C

05/31/2000 2 E

05/31/2000 3 F

05/31/2001 1 A

05/31/2001 2 B

05/31/2001 3 C

05/31/2001 4 D

05/31/2001 4 E

05/31/2001 4 F

Thank you all for your help in advance!


Respected Advisor
Posts: 3,777

Re: data manipulation

data cluster;
   input date:mmddyy. cluster_id code:$1.;
  
format date mmddyy10.;
  
cards;
05/31/2000 1 A
05/31/2000 1 B
05/31/2000 3 C
05/31/2000 3 E
05/31/2000 5 F
05/31/2001 1 A
05/31/2001 2 B
05/31/2001 5 C
05/31/2001 7 D
05/31/2001 7 E
05/31/2001 7 F
;;;;
   run;
data cluster;
   set cluster;
   by date cluster_id;
   if first.date then new_id = 0;
  
if first.cluster_id then new_id + 1;
  
run;
proc print;
  
run;
Super User
Posts: 5,085

Re: data manipulation

Perhaps this way:

data want;

   set have;

   by date cluster_id;

   if first.date then new_cluster_id = 1;

   else if first.cluster_id then new_cluster_id + 1;

run;

Contributor
Posts: 70

Re: data manipulation

Hi HDG..

We can do this using proc rank very easily ..here is the code for you..this may help for your requirement..

data cluster;

   input date:mmddyy. cluster_id code:$1.;

   format date mmddyy10.;

   cards;

05/31/2000 1 A

05/31/2000 1 B

05/31/2000 3 C

05/31/2000 3 E

05/31/2000 5 F

05/31/2001 1 A

05/31/2001 2 B

05/31/2001 5 C

05/31/2001 7 D

05/31/2001 7 E

05/31/2001 7 F

;;;;

   run;

   proc print;

   run;

proc rank data = cluster ties = dense  out = want;

by date;

var cluster_id;

ranks clstr_wnt;

run;

proc print data = want;

run;


Thanks,

Yash...

Ask a Question
Discussion stats
  • 3 replies
  • 215 views
  • 0 likes
  • 4 in conversation