Hi Everyone,
I require a sas macro for returning:
Thanks
Regards,
Why a macro?
What have you tried so far?
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).
%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()
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,
@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.
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)
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.