DATA Step, Macro, Functions and more

SAS Macro for top 10 and last 10 values for variable

Reply
New Contributor
Posts: 2

SAS Macro for top 10 and last 10 values for variable

Hi Everyone,

 

I require a sas macro for returning:

 

  1. top 10 and last 10 values of a numeric variable (Assume variable is not sorted).
  2. alphabetically top 10 and last 10 values of a character variable

 

Thanks

 

Regards,

 

 

Super User
Posts: 17,912

Re: SAS Macro for top 10 and last 10 values for variable

Why a macro?

What have you tried so far?

Super User
Posts: 10,538

Re: SAS Macro for top 10 and last 10 values for variable

What do you return if there are fewer than total observations?

Do missing or blank values get included?

Can the top 10 and bottom 10 overlap (fewer than 20 observations);

Define "top": largest (problematic for character) most frequent, alphabetic order(probematical for numeric).

 

 

Super User
Posts: 9,687

Re: SAS Macro for top 10 and last 10 values for variable

%macro top_end(data=sashelp.cars , var=make);
data want;
 set &data(keep=&var) nobs=nobs;
 if _n_ le 10 or 
    _n_ =nobs-9 or 
    _n_ =nobs-8 or
    _n_ =nobs-7 or
    _n_ =nobs-6 or
    _n_ =nobs-5 or
    _n_ =nobs-4 or
    _n_ =nobs-3 or
    _n_ =nobs-2 or
    _n_ =nobs-1 or
    _n_ =nobs ;
run;
proc print noobs;run;
%mend;

%top_end()
New Contributor
Posts: 2

Re: SAS Macro for top 10 and last 10 values for variable

[ Edited ]

Hi Xia,

 

Your macro gives us top 10 and bottom 10 values of a variable. Just three more enhancements:

 

1. Please can you add another option in the macro to make the n top and bottom values needed. for eg: for n=20, n=5, n=15, etc.

 

2. If there are missing values, delete the missing values.

 

3. Add the n% percentage option, for eg: pick top 10% and bottom 10% of values. 

 

Thanks

 

Regards,

 

Super User
Posts: 17,912

Re: SAS Macro for top 10 and last 10 values for variable


deepankeranand wrote:

Hi Xia,

 

Your macro gives us top 10 and bottom 10 values of a variable. Just three more enhancements:

 

1. Please can you add another option in the macro to make the n top and bottom values needed. for eg: for n=20, n=5, n=15, etc.

 

2. If there are missing values, delete the missing values.

 

3. Add the n% percentage option, for eg: pick top 10% and bottom 10% of values. 

 

Thanks

 

Regards,

 


Please try something, post what you've tried and what isn't working.

As phrased this isn't a request for help its a 'do my work'. If you need this type of service consider hiring a consultant. 

Super User
Posts: 9,687

Re: SAS Macro for top 10 and last 10 values for variable

As Reeza pointed out. Will you pay me some money ?


%macro top_end(data=sashelp.air , var=date,n=10,percent=);
%if %length(&percent) %then %do;
 %let dsid=%sysfunc(open(&data));
 %let nobs=%sysfunc(attrn(&dsid,nlobs));
 %let dsid=%sysfunc(close(&dsid));
 %let n=%sysevalf(&nobs*&percent,i);
%end;
data want;
 set &data(keep=&var) nobs=nobs;
 if _n_ le &n or 
    _n_ ge nobs-&n+1 ;
run;
proc print noobs;run;
%mend;

%top_end()
%top_end(percent=0.1)

Ask a Question
Discussion stats
  • 6 replies
  • 307 views
  • 2 likes
  • 4 in conversation