Search code examples
sas

Count the number of times record appears within a variable and apply count in a new variable


SAS - I'd like to count the number of times a record appears within a variable (Ref) and apply a count in a new variable (Count) for these.

Eg.

Ref Count
1000 1
1001 1
2000 1
3000 1
1000 2
1000 3

What is the best way to do this?


Solution

  • That is what PROC FREQ is for. It will count the number of OBSERVATIONS for each value of a variable (or combination of variables).

    proc freq data=have;
      tables REF ;
    run;
    

    If you want the result in a dataset then use the OUT= option of the TABLES statement.

    proc freq data=have;
      tables REF / out=want;
    run;