Hello All,
Below is the code.
options mprint symbolgen;
%macro highest(num=);
%global g_num;
%let g_num = #
proc sort data=have;
by descending salary;
run;
data want;
set have;
if _n_ = &g_num then do;
output;
stop;
end;
run;
proc print data = want;
%if &g_num = 1 %then %do;
title "This is 1st Highest Salary";
%end;
%else %if &g_num = 2 %then %do;
tilte "This is 2nd Highest Salary";
%end;
%else %if &g_num = 3 %then %do;
title "This is 3rd Highest Salary";
%end;
%else %do;
title "This is &g_num.th Highest Salary";
%end;
%symdel g_num;
%mend highest;
The program runs fine when I execute the macro call along with it's definition.
But, when I change the value in the macro parameter and execute the macro line alone, the log resolves correctly but the output prints previous result.
Would like to know what's wrong with the code above.
Any help is highly appreciated.
Thanks,
Vaibhav
You have a typo in one of the TITLE statements.
Your PROC PRINT is missing a RUN statement.
Otherwise your code should work, but if you don't follow the last call with either a RUN statement or another DATA or PROC step then SAS is still waiting for you to finish the PROC PRINT step so it can compile it and run it.
Here is version that takes advantage of the FIRSTOBS= and OBS= dataset options to eliminate the DATA step.
%macro highest(num);
%local ordinal;
%if &num = 1 %then %let ordinal=1st;
%else %if &num = 2 %then %let ordinal=2nd;
%else %if &num = 3 %then %let ordinal=3rd;
%else %let ordinal=&num.th;
proc sort data=have out=want;
by descending salary;
run;
proc print data = want(firstobs=&num obs=&num);
title "This is &ordinal. Highest Salary";
run;
%mend highest;
data have ;
input salary @@;
cards;
1 2 3 4 5
;
options mprint;
%highest(1)
%highest(2)
%highest(5)
Does adding a run; statement at the end of the macro help by any chance?.
This should work
%macro highest(num=);
proc sort data=HAVE;
by DESCENDING SALARY;
run;
title
%if &num = 1 %then "This is the 1st Highest Salary";
%else %if &num = 2 %then "This is the 2nd Highest Salary";
%else %if &num = 3 %then "This is the 3rd Highest Salary";
%else "This is the &num.th Highest Salary";
;
proc print data=HAVE (firstobs=&num. obs=&num.);
run;
%mend highest;
Thanks! But, got this,
ERROR 22-7: Invalid option name FIRST_OBS.
You have a typo in one of the TITLE statements.
Your PROC PRINT is missing a RUN statement.
Otherwise your code should work, but if you don't follow the last call with either a RUN statement or another DATA or PROC step then SAS is still waiting for you to finish the PROC PRINT step so it can compile it and run it.
Here is version that takes advantage of the FIRSTOBS= and OBS= dataset options to eliminate the DATA step.
%macro highest(num);
%local ordinal;
%if &num = 1 %then %let ordinal=1st;
%else %if &num = 2 %then %let ordinal=2nd;
%else %if &num = 3 %then %let ordinal=3rd;
%else %let ordinal=&num.th;
proc sort data=have out=want;
by descending salary;
run;
proc print data = want(firstobs=&num obs=&num);
title "This is &ordinal. Highest Salary";
run;
%mend highest;
data have ;
input salary @@;
cards;
1 2 3 4 5
;
options mprint;
%highest(1)
%highest(2)
%highest(5)
Wow! Great approach and explanation! Thanks a lot!
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.
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.