sas - find the count of num column changing by id -


please can me follwing probelm.

i have following dummy data:

id  num    1   1 1   2 1   1 1   2 1   1 1   2 2   1 2   15 2   1  2   1 2   1 2   15 2   1 2   15 

how count number of times num (column) changing each id? please find results , new column. need results this

id  number  no_of_times 1   1       1 1   2       1 1   1       1 1   2       2 1   1       1 1   2       3 2   1       1 2   15      1 2   1       1    2   1       1 2   1       1 2   15      2 2   1       1 2   15      3 

hope can understand after seeing results

the following hash approach works test data provided question:

data have; input id  number  no_of_times_target; cards; 1   1       1 1   2       1 1   1       1 1   2       2 1   1       1 1   2       3 2   1       1 2   15      1 2   1       1    2   1       1 2   1       1 2   15      2 2   1       1 2   15      3 ; run;  data want;   set have;   id;   if _n_ = 1 do;     length prev_number no_of_times 8;     declare hash h();     rc = h.definekey('number','prev_number');     rc = h.definedata('no_of_times');     rc = h.definedone();   end;   prev_number = lag(number);   if number > prev_number , not(first.id) do;     rc = h.find();     no_of_times = sum(no_of_times,1);     rc = h.replace();   end;   else no_of_times = 1;   if last.id rc = h.clear();   drop rc prev_number; run; 

Comments