## calculating quantiles using proc univariate?

Occasional Contributor
Posts: 5

# calculating quantiles using proc univariate?

Hello,

I have a dataset with thousands of distirbutions different on sample typles. I want to calculate quantiles for each distribution with specific quantile levels stored in a variable. The dataset is basically like this:

obs results sample_type pct

1        0.5          A              90

2         0.8          A             90

3        0.4           B              85

4        0.54         A              90

......

when I use

proc univariate data =mydata;
var result;
output out=out_quantile mean = mean pctlpts=pct  pctlpre = p pctlname = _lb _ub ;
by sample_type;
run;

It appears that the option "pctlpts" allows numbers only. you can not assign a variable to it even it is numeric variable.

Is there any ways that I can make it work?

Thanks!

Super User
Posts: 13,498

## Re: calculating quantiles using proc univariate?

PCTLPTS is the option to tell which percentiles you are interested in for all of the VAR variables and all values of the BY variables. If you want the the 10th, 15th and 60th percentiles in your output you use pctlpts= 10 15 60, the PCTLPRE and PCTLNAME are the bits you use to NAME the output variables.

You should post some example data, in the form of a datastep, and what you want for output for a small sample. Your example data should include enough values to calculate some of the desired percentiles and include at least 2 of your sample_type values.

It almost looks to me as if you may actually want to calculate a whole bunch of percentiles but only keep some of them which would require post processing the result.

Do you have different PCT values for the same Sample_type?

IF your PCT values were restricted to 1, 5, 10, 25, 50, 75, 90, 95 and 99 this might be practical in a single call to Proc tabulate but that procedure only does a limited set of percentiles.

Occasional Contributor
Posts: 5

## Re: calculating quantiles using proc univariate?

Thank you! Let me provide more details
Each sample type has a its own distribution and a specfic percentile I am interested in. For example, I want to know 90th
percentile for sample A and 80th percentile for Sample B. The numbers "80", "90" are stored as a variable called "pct".
Whan I calculate the percentiles, I want to point to that variable instead of directly typing those numbers.
Is it possible to do that?
ballardw wrote:

PCTLPTS is the option to tell which percentiles you are interested in for all of the VAR variables and all values of the BY variables. If you want the the 10th, 15th and 60th percentiles in your output you use pctlpts= 10 15 60, the PCTLPRE and PCTLNAME are the bits you use to NAME the output variables.

You should post some example data, in the form of a datastep, and what you want for output for a small sample. Your example data should include enough values to calculate some of the desired percentiles and include at least 2 of your sample_type values.

It almost looks to me as if you may actually want to calculate a whole bunch of percentiles but only keep some of them which would require post processing the result.

Do you have different PCT values for the same Sample_type?

IF your PCT values were restricted to 1, 5, 10, 25, 50, 75, 90, 95 and 99 this might be practical in a single call to Proc tabulate but that procedure only does a limited set of percentiles.

Super User
Posts: 23,662

## Re: calculating quantiles using proc univariate?

Smile_to_you wrote:
Thank you! Let me provide more details Each sample type has a its own distribution and a specfic percentile I am interested in. For example, I want to know 90th percentile for sample A and 80th percentile for Sample B. The numbers "80", "90" are stored as a variable called "pct".  Whan I calculate the percentiles, I want to point to that variable instead of directly typing those numbers.  Is it possible to do that?

As @ballardw indicated you can't specify the list dynamically for different variables, but you can calculate all and then filter out the ones you don't want.

Super User
Posts: 10,761

## Re: calculating quantiles using proc univariate?

```Post some more data would be better. and Post the output you want too.
I think you can try proc ranks.

CODE NOT TESTED
proc ranks data=have groups=100 out=temp;
by sample_type;
var results;
rank rank;
run;
data want;
set temp;
if pct=rank;
run;

```
Discussion stats
• 4 replies
• 979 views
• 0 likes
• 4 in conversation