An Alternative Approach to Create Quintiles in SAS

The code below shows how one can use some advanced data step techniques to create quintiles (or any -iles) in SAS. One can of course use PROC RANK but that creates a new dataset that may need to be merged to  the original. In this case one does not need to do any merging.

Note how we load an auxiliary dataset with the breaks for the deciles on the first iteration of the DATA step.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    <http://www.gnu.org/licenses/>.
    Developed by Mario Segal

proc means data=mydataset  noprint;

var desired_var;
output out=breaks p20(desired_var)=p20 p40(desired_var)=p40 p60(desired_var)=p60 p80(desired_var)=p80;

run;

data mydataset;

if _N_ eq 1 then do;
set breaks (drop=_freq_ _type_);
array breaks{4} _temporary_ ;
breaks{1} = p20; breaks{2} = p40; breaks{3}=p60; breaks{4}=p80;
end;

set mydataset;

select ;
when (desired_var le breaks{1}) quintile=5;
when (desired_var gt breaks{1} and desired_var le breaks{2}) quintile=4;
when (desired_var gt breaks{2} and desired_var le breaks{3}) quintile=3;
when (desired_var gt breaks{3} and desired_var le breaks{4}) quintile=2;
when (desired_var gt breaks{4}) quintile=1;
end;

drop p: ;

run;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s