I have a dataset for which I want to know the p25, p50, and p75 (and maybe other) percentiles for var1 (first variable) when the data is grouped by var2 (second variable). I was able to do this using PROC UNIVARIATE but I then lose my original data once I output from PROC UNIVARIATE. Is there a way to calculate the percentiles and merge it back with my original dataset?
Here's an illustration of two different methods:
https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas
Another option, if you're trying to bin groups with percentiles is to use the RANK procedure instead.
@benvar wrote:
I have a dataset for which I want to know the p25, p50, and p75 (and maybe other) percentiles for var1 (first variable) when the data is grouped by var2 (second variable). I was able to do this using PROC UNIVARIATE but I then lose my original data once I output from PROC UNIVARIATE. Is there a way to calculate the percentiles and merge it back with my original dataset?
Example:
proc summary data=have nway;
class var2;
var var1;
output out=pctiles p25=p25 p50=p50 p75=75;
run;
proc sort data=have;
by var2;
run;
data want;
merge have pctiles;
by var2;
run;
You can't attach it as part of PROC UNIVARIATE. Run that first, then put the results back onto your original data set ... either MERGE by VAR2, or use SQL, matching on VAR2.
Here's an illustration of two different methods:
https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas
Another option, if you're trying to bin groups with percentiles is to use the RANK procedure instead.
@benvar wrote:
I have a dataset for which I want to know the p25, p50, and p75 (and maybe other) percentiles for var1 (first variable) when the data is grouped by var2 (second variable). I was able to do this using PROC UNIVARIATE but I then lose my original data once I output from PROC UNIVARIATE. Is there a way to calculate the percentiles and merge it back with my original dataset?
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.