BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

I know how to calculate percentiles of specific variable.

for exampe:


proc univariate data =sashelp.cars noprint;
var invoice;
output out=outdata PCTLPTS = 5 to 100 by 5  PCTLPRE = P;
run;

My question;

Let's say that I want to know what percentile is value 50000.

How can I answer it via sas code please?

 

 

2 REPLIES 2
PaigeMiller
Diamond | Level 26
proc rank data=sashelp.cars out=cars_ranks percent;
var invoice;
ranks invoice_r;
run;

proc sort data=cars_ranks;
    by invoice;
run;

data want;
    set cars_ranks;
    prev_invoice=lag(invoice);
    if prev_invoice<50000 and invoice>=50000 then output;
    keep invoice invoice_r;
run;

 

So 50000 is percentile 91.589 (rounded-off to 3 decimal places).

--
Paige Miller
mkeintz
PROC Star

In addition to the proc rank approach, there is a straightforward data step approach:

 

data _null_;
  set sashelp.cars (keep=invoice) end=end_of_data;
  array freqs {-1:1} _temporary_ (3*0);
  freqs{sign(invoice-50000)}+1;

  if end_of_data;
  pctile_50000 = freqs{-1} / sum(of freqs{*});
  put pctile_50000=percent10.4;  
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
  • 2 replies
  • 1032 views
  • 4 likes
  • 3 in conversation