BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
arunvaibhav2
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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)

View solution in original post

7 REPLIES 7
ChrisNZ
Tourmaline | Level 20

Does  adding a run; statement at the end of the macro help by any chance?.

arunvaibhav2
Calcite | Level 5
Thanks a lot!! That worked!
ChrisNZ
Tourmaline | Level 20

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;

 

 

arunvaibhav2
Calcite | Level 5

Thanks! But, got this,

 

ERROR 22-7: Invalid option name FIRST_OBS.

Tom
Super User Tom
Super User

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)
arunvaibhav2
Calcite | Level 5

Wow! Great approach and explanation! Thanks a lot! 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 7 replies
  • 918 views
  • 1 like
  • 3 in conversation