Robert Penridge
Robert Penridge

Reputation: 8513

'Unordered' by group processing in SAS

Is there a way to use by group processing in SAS when the data is grouped together but is out of order?

data sample;
  input x;
  datalines;
3
3
1
1
2
2
;
run;

Try to print out the first of each group:

data _null_;
  set sample;
  by x;
  if first.x then do;
    put _all_;
  end;
run;

Results in the below error:

x=3 FIRST.x=1 LAST.x=0 _ERROR_=0 _N_=1
ERROR: BY variables are not properly sorted on data set WORK.SAMPLE.
x=3 FIRST.x=1 LAST.x=0 _ERROR_=1 _N_=2
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 3 observations read from the data set WORK.SAMPLE.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds

And just to reiterate - I do not want to sort the grouped data first - I need to process it in this order. I know I could create a proxy variable to sort on using an intermediary datastep and either a retain statement or the lag() function but I'm really looking for a solution that avoids this step. Also, I'd like to use the first and last keywords in my by-group processing.

Upvotes: 0

Views: 629

Answers (1)

BellevueBob
BellevueBob

Reputation: 9618

Use the NOTSORTED option on your BY statement:

data _null_;
   set sample;
     by x NOTSORTED;
   if first.x then do;
      put _all_;
      end;
run;

Upvotes: 5

Related Questions