Search code examples
sqlsaslimit

How to get the 1st occurrence of Sex (male & female both) from sashelp.class


I am trying to get all the details in a row for the first occurence of sex='m' and sex='f' from sashelp.class.

Sex is a column in sashelp.class

Till now I have done this:

Proc sql;
Select *
From sashelp.class
Where sex='m' or sex='f'
Order by sex
Limit 1;
Quit;

This gives me an error in the limit statement. Saying "Syntax Error, expecting on of the following: !, !! ... etc"


Solution

  • Here is an easy to follow data step approach.

    data want;
       set sashelp.class;
       if sex = 'M' and m = . then do;
          output; m = 1; 
       end;
       if sex = 'F' and f = . then do;
          output; f = 1; 
       end;
       retain m f;
       drop m f;
    run;