BookmarkSubscribeRSS Feed
deepankeranand
Calcite | Level 5

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,

 

 

6 REPLIES 6
Reeza
Super User

Why a macro?

What have you tried so far?

ballardw
Super User

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).

 

 

Ksharp
Super User
%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()
deepankeranand
Calcite | Level 5

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,

 

Reeza
Super User

@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. 

Ksharp
Super User
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)

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1304 views
  • 2 likes
  • 4 in conversation