Search code examples
sasdataset

By statement in sas


I have the following data with three groups.

data draft_us;
input group$ ID$ SEX$ Value;
datalines;
G1 010 Male 30
G1 009 Female 35
G1 009 Male 20
G3 009 Male 44
G3 009 Female 0
G3 009 Male 45
G2 009 Male 50
G2 009 Female 100
;
run;

Why the by statement is not working in data steps. I have tried this code

data set_draft;
set draft_us;
by SEX;
run; 

Here is the error message

ERROR: BY variable ascending is not on input data set
       WORK.DRAFT_US.
NOTE: The SAS System stopped processing this step because of
      errors.
WARNING: The data set WORK.SET_DRAFT may be incomplete.  When
         this step was stopped there were 0 observations and 4
         variables.

Solution

  • Your example data is not sorted by SEX. The first observation is Male, the second is Female which comes before Male in sort order. If you tell the data step the data is sorted and it isn't then you get a different error message than the one you posted.

    ERROR: BY variables are not properly sorted on data set WORK.DRAFT_US.

    Example LOG:

    2957  data set_draft;
    2958  set draft_us;
    2959  by SEX;
    2960  run;
    
    ERROR: BY variables are not properly sorted on data set WORK.DRAFT_US.
    group=G1 ID=010 SEX=Male Value=30 FIRST.SEX=1 LAST.SEX=1 _ERROR_=1 _N_=1
    NOTE: The SAS System stopped processing this step because of errors.
    WARNING: The data set WORK.SET_DRAFT may be incomplete.  When this step was stopped there were 0 observations and 4 variables.
    NOTE: DATA statement used (Total process time):
          real time           0.00 seconds
          cpu time            0.00 seconds
    

    If you want to process the data using BY groups (which your simple example is NOT doing) then it needs to actually be organized into those groups. For example by adding a PROC SORT step.