## how to trim numerical variables in sas?

Solved
Occasional Contributor
Posts: 6

# how to trim numerical variables in sas?

Hello,

I have a sample including the variables of Return, Earnings and Size from 1980 to 2015. I want to remove (delete) the higher and lower 1% of each variable's observations each year using SAS 9.4. however, I am not sure about the codes I should use.

Accepted Solutions
Solution
‎09-27-2017 05:00 AM
Super User
Posts: 10,787

## Re: how to trim numerical variables in sas?

The following delete the top and bottom 20% data for each year.

If you need 1% change GROUPS=100 and NOT IN (0 99) .

``````data air;
set sashelp.air;
year=year(date);
drop date;
run;
proc rank data=air out=temp groups=5;
by year;
var air;
ranks r_air;
run;
data want;
set temp;
if r_air not in (0 4);
run;``````

All Replies
Regular Contributor
Posts: 249

## Re: how to trim numerical variables in sas?

Occasional Contributor
Posts: 6

## Re: how to trim numerical variables in sas?

thank you so much. can you please advise me how to extend the code to do this for each year?
Solution
‎09-27-2017 05:00 AM
Super User
Posts: 10,787

## Re: how to trim numerical variables in sas?

The following delete the top and bottom 20% data for each year.

If you need 1% change GROUPS=100 and NOT IN (0 99) .

``````data air;
set sashelp.air;
year=year(date);
drop date;
run;
proc rank data=air out=temp groups=5;
by year;
var air;
ranks r_air;
run;
data want;
set temp;
if r_air not in (0 4);
run;``````
Contributor
Posts: 29

## Re: how to trim numerical variables in sas?

@Ksharp This is a good approach. But Proc Rank only works when obersvation numbers are larger than GROUPS size. I did a draft based on the reference by @wong

``````data air;
set sashelp.retail;
rename sales=air;
drop date;
run;

proc means data=air noprint;
by year;
var air;
output out=stat
p1=p1
p99=p99;
run;

data r_air;
merge air(in=x) stat(keep=year p1 p99);
by year;
if x;
run;

data want;
set r_air;
if p1<=air<=p99;
run;``````
Occasional Contributor
Posts: 6

## Re: how to trim numerical variables in sas?

thank you all. I appreciate your help.

I tried Proc Rank and it worked.

☑ This topic is solved.

Discussion stats
• 5 replies
• 755 views
• 6 likes
• 4 in conversation