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?
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: