Search code examples
reportsasdatastep

Specifying Order of Variables for SAS Report


I am working on a project where I need to specify the order of objects in the data for a custom SAS report. I am having trouble with something that should be easy, here is an example of the data I am working with.

obs   ord   ord2  name
  1     3      1    A
  2     3      .    B
  3     3      .    C
  4     3      .    D
  5     4      1    E
  6     4      .    F
  7     5      1    G
  8     5      .    H
  9     5      .    I
 10     5      .    J

What I'd like is...

    obs   ord   ord2  name
      1     3      1    A
      2     3      2    B
      3     3      3    C
      4     3      4    D
      5     4      1    E
      6     4      2    F
      7     5      1    G
      8     5      2    H
      9     5      3    I
     10     5      4    J

So that for every first occurrence of ord, ord2 = 1,...,n_i.

Thanks for the help!


Solution

  • Just apply a group numbering to the original data set, provided that the table has been sorted by ord.

    data table1;
       set table1;
       by ord;
       ord2_ + 1;
       if first.ord then ord2_ = 1;
       drop ord2;
       rename ord2_=ord2;
    Run;