<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Need help in Macro Error - Macro prints previous result! in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-help-in-Macro-Error-Macro-prints-previous-result/m-p/413870#M101342</link>
    <description>&lt;P&gt;This should work&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro highest(num=);

  proc sort data=HAVE;
    by DESCENDING SALARY;
  run;

  title 
  %if       &amp;amp;num = 1 %then "This is the 1st Highest Salary";
  %else %if &amp;amp;num = 2 %then "This is the 2nd Highest Salary";
  %else %if &amp;amp;num = 3 %then "This is the 3rd Highest Salary";
  %else                    "This is the &amp;amp;num.th Highest Salary";
  ;

  proc print data=HAVE (firstobs=&amp;amp;num. obs=&amp;amp;num.);
  run;

%mend highest;

 

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 16 Nov 2017 05:20:54 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2017-11-16T05:20:54Z</dc:date>
    <item>
      <title>Need help in Macro Error - Macro prints previous result!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-in-Macro-Error-Macro-prints-previous-result/m-p/413860#M101337</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is the code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint symbolgen;
%macro highest(num=);
	%global g_num;
	%let g_num = &amp;amp;num;

	proc sort data=have;
		by descending salary;
	run;

	data want;
		set have;
	if _n_ = &amp;amp;g_num then do;
  		output;
  		stop;
	end;
	run;

	proc print data = want;
	%if &amp;amp;g_num = 1 %then %do;
		title "This is 1st Highest Salary";
	%end;
	%else %if &amp;amp;g_num = 2 %then %do; 
		tilte "This is 2nd Highest Salary";
	%end;
	%else %if &amp;amp;g_num = 3 %then %do;
		title "This is 3rd Highest Salary";
	%end;
	%else %do;
		title "This is &amp;amp;g_num.th Highest Salary";
	%end;

%symdel g_num;

%mend highest;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The program runs fine when I execute the macro call along with it's definition.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would like to know what's wrong with the code above.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is highly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Vaibhav&lt;/P&gt;</description>
      <pubDate>Thu, 16 Nov 2017 01:42:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-in-Macro-Error-Macro-prints-previous-result/m-p/413860#M101337</guid>
      <dc:creator>arunvaibhav2</dc:creator>
      <dc:date>2017-11-16T01:42:52Z</dc:date>
    </item>
    <item>
      <title>Re: Need help in Macro Error - Macro prints previous result!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-in-Macro-Error-Macro-prints-previous-result/m-p/413868#M101340</link>
      <description>&lt;P&gt;Does&amp;nbsp; adding a&lt;FONT face="courier new,courier"&gt; run;&lt;/FONT&gt; statement at the end of the macro help by any chance?.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Nov 2017 02:58:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-in-Macro-Error-Macro-prints-previous-result/m-p/413868#M101340</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-11-16T02:58:32Z</dc:date>
    </item>
    <item>
      <title>Re: Need help in Macro Error - Macro prints previous result!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-in-Macro-Error-Macro-prints-previous-result/m-p/413870#M101342</link>
      <description>&lt;P&gt;This should work&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro highest(num=);

  proc sort data=HAVE;
    by DESCENDING SALARY;
  run;

  title 
  %if       &amp;amp;num = 1 %then "This is the 1st Highest Salary";
  %else %if &amp;amp;num = 2 %then "This is the 2nd Highest Salary";
  %else %if &amp;amp;num = 3 %then "This is the 3rd Highest Salary";
  %else                    "This is the &amp;amp;num.th Highest Salary";
  ;

  proc print data=HAVE (firstobs=&amp;amp;num. obs=&amp;amp;num.);
  run;

%mend highest;

 

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Nov 2017 05:20:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-in-Macro-Error-Macro-prints-previous-result/m-p/413870#M101342</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-11-16T05:20:54Z</dc:date>
    </item>
    <item>
      <title>Re: Need help in Macro Error - Macro prints previous result!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-in-Macro-Error-Macro-prints-previous-result/m-p/413872#M101344</link>
      <description>&lt;P&gt;You have a typo in one of the TITLE statements.&lt;/P&gt;
&lt;P&gt;Your PROC PRINT is missing a RUN statement.&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is version that takes advantage of the FIRSTOBS= and OBS= dataset options to eliminate the DATA step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro highest(num);
%local ordinal;
%if &amp;amp;num = 1 %then %let ordinal=1st;
%else %if &amp;amp;num = 2 %then %let ordinal=2nd;
%else %if &amp;amp;num = 3 %then %let ordinal=3rd;
%else %let ordinal=&amp;amp;num.th;

proc sort data=have out=want;
  by descending salary;
run;

proc print data = want(firstobs=&amp;amp;num obs=&amp;amp;num);
  title "This is &amp;amp;ordinal. Highest Salary";
run;
%mend highest;

data have ;
  input salary @@;
cards;
1 2 3 4 5
;

options mprint;
%highest(1)
%highest(2)
%highest(5)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Nov 2017 03:20:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-in-Macro-Error-Macro-prints-previous-result/m-p/413872#M101344</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-11-16T03:20:40Z</dc:date>
    </item>
    <item>
      <title>Re: Need help in Macro Error - Macro prints previous result!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-in-Macro-Error-Macro-prints-previous-result/m-p/413890#M101357</link>
      <description>&lt;P&gt;Wow! Great approach and explanation! Thanks a lot!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Nov 2017 04:55:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-in-Macro-Error-Macro-prints-previous-result/m-p/413890#M101357</guid>
      <dc:creator>arunvaibhav2</dc:creator>
      <dc:date>2017-11-16T04:55:26Z</dc:date>
    </item>
    <item>
      <title>Re: Need help in Macro Error - Macro prints previous result!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-in-Macro-Error-Macro-prints-previous-result/m-p/413891#M101358</link>
      <description>Thanks a lot!! That worked!</description>
      <pubDate>Thu, 16 Nov 2017 04:57:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-in-Macro-Error-Macro-prints-previous-result/m-p/413891#M101358</guid>
      <dc:creator>arunvaibhav2</dc:creator>
      <dc:date>2017-11-16T04:57:28Z</dc:date>
    </item>
    <item>
      <title>Re: Need help in Macro Error - Macro prints previous result!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-in-Macro-Error-Macro-prints-previous-result/m-p/413892#M101359</link>
      <description>&lt;P&gt;Thanks! But, got this,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR 22-7: Invalid option name FIRST_OBS.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Nov 2017 05:01:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-in-Macro-Error-Macro-prints-previous-result/m-p/413892#M101359</guid>
      <dc:creator>arunvaibhav2</dc:creator>
      <dc:date>2017-11-16T05:01:47Z</dc:date>
    </item>
    <item>
      <title>Re: Need help in Macro Error - Macro prints previous result!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-in-Macro-Error-Macro-prints-previous-result/m-p/413894#M101361</link>
      <description>&lt;P&gt;Typo &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Fixed.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Nov 2017 05:21:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-in-Macro-Error-Macro-prints-previous-result/m-p/413894#M101361</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-11-16T05:21:16Z</dc:date>
    </item>
  </channel>
</rss>

