BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BonnaryW
Obsidian | Level 7

Hello,

I am so new to SAS progam and need help with my codes below.  I am trying to find the top 10% of employees with highest salary.

DATA TOP10PS; SET SAS.T;

PROC SORT; BY DESCENDING SALARY;

DATA EE1; SET TOP10PS END=EOF;

IF EOF THEN DO;

FORMAT TENP 7.;

COUNT=_N_;

TENP = COUNT / 10;

IF COUNT <= TENP THEN OUTPUT;

END;

KEEP EE RACE SEX SALARY;

RUN;

DATA FINAL; SET EE1;

PROC PRINT N; ID EE; VAR RACE SEX SALARY;

RUN;

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Your code wouldn't be recognized, by SAS, as being valid code. Conversely, the following would do what you want.

When you run a querry using SAS proc sql, &sqlobs. contains the number of records in the file:

 

/*as an example I'm substituting sashelp.class for sas.t*/
proc sql noprint;
  create table final as
    select *
      from sashelp.class (rename=(name=EE
                                  age=RACE
                                  weight=SALARY))
        order by salary descending
  ;
quit;

data final (keep=EE RACE SEX SALARY);
  set final;
  if _n_/&sqlobs. gt .1 then stop;
  output;
run;

proc print;
run;

HTH,

Art, CEO, AnalystFinder.com

 

View solution in original post

4 REPLIES 4
art297
Opal | Level 21

Your code wouldn't be recognized, by SAS, as being valid code. Conversely, the following would do what you want.

When you run a querry using SAS proc sql, &sqlobs. contains the number of records in the file:

 

/*as an example I'm substituting sashelp.class for sas.t*/
proc sql noprint;
  create table final as
    select *
      from sashelp.class (rename=(name=EE
                                  age=RACE
                                  weight=SALARY))
        order by salary descending
  ;
quit;

data final (keep=EE RACE SEX SALARY);
  set final;
  if _n_/&sqlobs. gt .1 then stop;
  output;
run;

proc print;
run;

HTH,

Art, CEO, AnalystFinder.com

 

BonnaryW
Obsidian | Level 7
Hello Art,
Thank you so much and very grateful for your assistance. ~Bonnary
mkeintz
PROC Star

You could use proc rank to get the top decile first.  Then you can sort that much smaller data set without further filtering. 

 

proc rank data=sas.t 
                out=final (where=(salary_decile=9)) 
                groups=10;
  var salary;
  ranks salary_decile;
run;

proc sort;
  by descending salary;
run;

proc print n;
  id ee;
  var race sex salary;
run;

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 21062 views
  • 0 likes
  • 4 in conversation