Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Analytics
- /
- Stat Procs
- /
- Re: Calculating Percentile across observations and excluding missing v...

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 03-01-2016 03:11 PM
(3293 views)

Hi all,

I'm wondering whether there is a way to calculate percentile across variables for each observation, and excludes missing values in the calculation?

The percentile function seems to work but if there is just a single missing value within all the variables then the result is a missing variable.

Only workaround I was able to find was to achieve this in Excel, as my dataset fortunately wasn't too large.

Thanks for your help!

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thanks!

The corrected code is below and still does not replicate the problem:

```
data test;
array pct(100) pct1-pct100 (1:100);
x=pctl(50, of pct(*));
do i=20 to 30;
pct(i)=.;
end;
y=pctl(50, of pct(*));
z=pctl(50, of pct1--pct100);
q=pctl(50, pct1, pct2, pct3, pct21, pct25, pct29, pct30, pct50, pct99, pct100);
keep x y z q;
run;
proc print;run;
```

@tts Have you been able to replicate your issue?

5 REPLIES 5

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

I would recommend a transpose of your data and then using a more robust procedure such as proc univariate or proc means to calculate the percentile.

However, the behaviour your describing is not consistent with the percentile documentation, which states:

The PCTL function returns the percentile of the __ nonmissing values__ corresponding to the percentage. I can't replicate that behaviour as well, can you post a sample of your data where this was occuring?

```
data test;
array pct(100) pct1-pct100 (1:100);
x=pctl(50, of pct(*));
do i=20 to 30;
pct(i)=.;
end;
y=pctl(50, of pct(*));
keep x y;
run;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

My data is structured similarly to your test dataset, however my variable names are not standardized. There is no standardization as they are various company tickers on NASDAQ.

The way you define the variables to calculate percentile across would not work in my instance, correct?

What I had tried was :

percentile= PCTL(95,A--VLU); with A and VLU being the first and last variables I am interested in calculating percentile across.

Thanks!

The way you define the variables to calculate percentile across would not work in my instance, correct?

What I had tried was :

percentile= PCTL(95,A--VLU); with A and VLU being the first and last variables I am interested in calculating percentile across.

Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

If you can define it that way, you could also define your array similarily.

`array stocks(*) A--VLU;`

At any rate, I still can't replicate your issue. Please post your code and sample data that replicates your problem. I'm guessing you actually have some other issue.

```
data test;
array pct(100) pct1-pct100 (1:100);
x=pctl(50, of pct(*));
do i=20 to 30;
pct(i)=.;
end;
y=pctl(50, of pct(*));
z=pctl(95, pct1--pct100);
keep x y z;
run;
proc print;run;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Hi @Reeza,

I think you should insert an "of" into your definition of z. Otherwise, the 95th percentile of a single value (singleton set), pct1--pct100=pct1+pct100=101, will be calculated.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thanks!

The corrected code is below and still does not replicate the problem:

```
data test;
array pct(100) pct1-pct100 (1:100);
x=pctl(50, of pct(*));
do i=20 to 30;
pct(i)=.;
end;
y=pctl(50, of pct(*));
z=pctl(50, of pct1--pct100);
q=pctl(50, pct1, pct2, pct3, pct21, pct25, pct29, pct30, pct50, pct99, pct100);
keep x y z q;
run;
proc print;run;
```

@tts Have you been able to replicate your issue?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. **Registration is now open through August 30th**. Visit the SAS Hackathon homepage.

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.