DATA Step, Macro, Functions and more

Need help in Macro Error - Macro prints previous result!

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Need help in Macro Error - Macro prints previous result!

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


Accepted Solutions
Solution
‎11-16-2017 12:02 AM
Super User
Super User
Posts: 8,272

Re: Need help in Macro Error - Macro prints previous result!

Posted in reply to arunvaibhav2

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


All Replies
Super User
Posts: 2,504

Re: Need help in Macro Error - Macro prints previous result!

Posted in reply to arunvaibhav2

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

Occasional Contributor
Posts: 6

Re: Need help in Macro Error - Macro prints previous result!

Thanks a lot!! That worked!
Super User
Posts: 2,504

Re: Need help in Macro Error - Macro prints previous result!

[ Edited ]
Posted in reply to arunvaibhav2

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;

 

 

Occasional Contributor
Posts: 6

Re: Need help in Macro Error - Macro prints previous result!

Thanks! But, got this,

 

ERROR 22-7: Invalid option name FIRST_OBS.

Super User
Posts: 2,504

Re: Need help in Macro Error - Macro prints previous result!

Posted in reply to arunvaibhav2

Typo Smiley Happy 

 

Fixed.

Solution
‎11-16-2017 12:02 AM
Super User
Super User
Posts: 8,272

Re: Need help in Macro Error - Macro prints previous result!

Posted in reply to arunvaibhav2

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)
Occasional Contributor
Posts: 6

Re: Need help in Macro Error - Macro prints previous result!

Wow! Great approach and explanation! Thanks a lot! 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 146 views
  • 1 like
  • 3 in conversation