2

I have such SAS statement:

proc univariate data = df noprint;
class &var1. &var2.;
var &var3.;
output out = STAT PCTLPTS = 2 5 98 99 95 PCTLPRE = P;

I have output from SAS proc like this:

enter image description here

How can I get the same result in R? (with 5 P-columns and values ​​for each row)

red_quark
  • 971
  • 5
  • 20

1 Answers1

1

Check out the quantile() function. https://stat.ethz.ch/R-manual/R-devel/library/stats/html/quantile.html

There is an underlying distribution quantile calculation type based on the SAS method.

Type 3 - SAS definition: nearest even order statistic. γ = 0 if g = 0 and j is even, and 1 otherwise.

Use the function like this:

quantile(iris$Sepal.Length, probs = c(.02, .05, .98, .99, .95), type=3)
 2%  5% 98% 99% 95% 
4.4 4.6 7.7 7.7 7.2 

You can use various methods, such as sapply() to execute quantile() on multiple variables. ex.

 t(sapply(iris[1:4], quantile, probs = c(.02, .05, .98, .99, .95), type=3))
              2%  5% 98% 99% 95%
Sepal.Length 4.4 4.6 7.7 7.7 7.2
Sepal.Width  2.2 2.3 4.0 4.1 3.8
Petal.Length 1.2 1.3 6.6 6.7 6.1
Petal.Width  0.1 0.2 2.4 2.5 2.3
M.Viking
  • 5,067
  • 4
  • 17
  • 33