Nupur
Nupur

Reputation: 357

proc rank in sas

my data set has two variables ID and diagnosis. I am trying to assign row numbers in my dataset based on ID and diagnosis.

the code that I used was;

proc sort data = temp;
by ID diagnosis;
run;

proc rank data = temp out  = temp1;
by id;
var diagnosis;
ranks = diag_rank;
run;

Its giving mr error: the variable diagnosis in list does not match the type

I know my diagnosis has both text and numric values. Is there a way to fix this. Thanks a lot.

Upvotes: 1

Views: 6030

Answers (3)

If you just want to use row number, you can use the default variable "_ N_" without quotes and space.

If it is conditional, you can follow Rwill's answer..

Upvotes: 0

RWill
RWill

Reputation: 949

If you are just looking for a unique row number, then the following should work.

data temp;
   set temp;
   retain row_number 0;
   row_number + 1;
run;

If you need a conditional row number based on ID and diagnosis, then try:

data temp;
   set temp;
   by ID diagnosis;
   retain row_number;
   if first.ID then do;
      row_number = 0;
   end;
   row_number + 1;
run;

If, on the other hand, there is some explicit order of ranking that you wish to impose on the DIAGNOSIS variable other than the sorted character values, then you could use a user-defined format to map each of the character values to a number, then use this format to create a row_number variable in a data step, like the following.

proc format;
   value $diag_row
   "67"   = 2
   "A234" = 4
   "B45"  = 3
   "V456" = 1
   other  = 999
   ;
run;

data temp;
   set temp;
   format row_number 8.0;
   row_number = put(diagnosis,$diag_row.);
run;

Upvotes: 3

RWill
RWill

Reputation: 949

Nupur, the VAR statement expects only numeric variables. If DIAGNOSIS is defined as character but only includes numeric values, then use an INFORMAT statement to create a new numeric variable. Add the following to the beginning of your example and it should work:

data temp;
   set temp(rename=(diagnosis=diag_char));
   format diagnosis 8.0;
   diagnosis = input(diag_char , 8.0);
   drop diag_char;
run;

However, if your DIAGNOSIS includes character values, then you will need to either reclassify the diagnosis or possibly translate the values to numeric values (if it makes sense).

Upvotes: 0

Related Questions