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

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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