<?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 Macro errors in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-errors/m-p/818783#M323203</link>
    <description>&lt;P&gt;Dear Users,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used a macro below for finding the next benchmark when returns do not exist.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am facing the errors attached in the log file.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let nindexmin=5 /*I set arbitrary number greater than zero*/;

%macro findnextbenchmark;
%DO %WHILE (&amp;amp;nindex&amp;gt;0 and &amp;amp;nindexmin&amp;gt;0);
/*First time*/
data MinBench;
     set FirmsBench6;
      by cusip;
      /* Prevents LOWEST from being reset*/
      /* to missing for each iteration.   */
      retain minimum;
      /* Sets HIGHEST to missing for each */
      /* different ID value.              */
      If first.cusip then minimum=.;
      /* Compares lowest to diff of BMs in  */
      /* current iteration and resets     */
      /* value if Diff of BM is lower.        */
      minimum=min(minimum,Diff_BM);
      if first.cusip then output;
run;

proc sql; 
     create table temp as 
	 select *
	 from missing a
	 left join  MinBench b
	 on a.cusip=b.cusip;
quit;

proc sort data=temp out=fdf nodupkeys;
     by cusip;
run;

data temp(keep=CUSIP Conditional_Exercise_Date cusip_ben date_ipo retx_ipo countdays);
     set temp;
run;

proc sort data=temp;
     by cusip countdays;
run;

/*Updated*/
proc sql;
     create table FirmsBench6 as
	 select  a.*
	 from FirmsBench6 a
     left join  MinBench b
	 on a.cusip=b.cusip  
     where a.cusip_ben not in (select b.cusip_ben from MinBench);
quit;

proc sort data= FirmsBench6;
      by CUSIP ;
run;


%let psw=xxxxxxxx;
%let username=xxxxxxxx;

/* 	Push the macro variables to the remote server (without the next line the WRDS server 
	would not 'understand' &amp;amp;vars etc); */
%let wrds = wrds.wharton.upenn.edu 4016;options comamid = TCP remote=WRDS;
signon  noscript user=xxxxx'  password="&amp;amp;psw";

rsubmit;
libname crsplib '/wrds/crsp/sasdata/a_stock';
proc upload data=temp  out=work.temp ; run; 

/*Get returns after conditional exercise date*/
PROC SQL;
       create table temp_1 as
       select a.* , b.retx as retx_ben
       from work.temp  a 
       left join crsplib.dsf b
  	   on a.cusip_ben=b.cusip and a.date_ipo=b.date;
quit;

proc download data=temp_1 out=temp_1;
run;
endrsubmit;
signoff;


data temp_2/*(drop=retx_ben1)*/;
     set temp_1;
	 if retx_ben ne .;
     if retx_ben ne .B;
     if retx_ben ne .C;
	 *retx_ben1=retx_ben+0;
	 *if retx_ben1 ne . ;
run;

proc append base=styled_base data=temp_2;
run;

proc sort data=styled_base;
     by cusip date_ipo;
run;

data Missing(drop=cusip_ben retx_ben);
     set temp_1;
	 if retx_ben= . or retx_ben=.B or retx_ben=.C;
run;

proc sort data=Missing out=Missigben nodupkeys;
     by cusip;
run;

data _null_;
	call symput('nindex',put(obs,best.));
	stop;
	set Missigben nobs=obs;
run;


data _null_;
	call symput('nindexmin',put(obs,best.));
	stop;
	set WORK.FIRMSBENCH6 nobs=obs;
run;
*%if &amp;amp;nindexmin = 0 %then LEAVE; 

%put &amp;amp;nindex;
%put &amp;amp;nindexmin;
%END;
%mend;
%findnextbenchmark;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;File Error:&lt;/P&gt;
&lt;P&gt;91&lt;BR /&gt;20688&lt;BR /&gt;MPRINT(FINDNEXTBENCHMARK): * data MinBench;&lt;BR /&gt;NOTE: Line generated by the invoked macro "FINDNEXTBENCHMARK".&lt;BR /&gt;1054 data MinBench; set FirmsBench6;&lt;BR /&gt;---&lt;BR /&gt;180&lt;BR /&gt;MPRINT(FINDNEXTBENCHMARK): set FirmsBench6;&lt;/P&gt;
&lt;P&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/P&gt;
&lt;P&gt;NOTE: Line generated by the invoked macro "FINDNEXTBENCHMARK".&lt;BR /&gt;1054 by cusip;&lt;BR /&gt;--&lt;BR /&gt;180&lt;BR /&gt;MPRINT(FINDNEXTBENCHMARK): by cusip;&lt;/P&gt;
&lt;P&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/P&gt;
&lt;P&gt;NOTE: Line generated by the invoked macro "FINDNEXTBENCHMARK".&lt;BR /&gt;1054 retain minimum;&lt;BR /&gt;------&lt;BR /&gt;180&lt;BR /&gt;MPRINT(FINDNEXTBENCHMARK): retain minimum;&lt;/P&gt;
&lt;P&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/P&gt;
&lt;P&gt;NOTE: Line generated by the invoked macro "FINDNEXTBENCHMARK".&lt;BR /&gt;1054 If first.cusip then minimum=.;&lt;/P&gt;
&lt;P&gt;--&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;many thanks in advance&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 17 Jun 2022 10:47:18 GMT</pubDate>
    <dc:creator>georgel</dc:creator>
    <dc:date>2022-06-17T10:47:18Z</dc:date>
    <item>
      <title>Macro errors</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-errors/m-p/818783#M323203</link>
      <description>&lt;P&gt;Dear Users,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used a macro below for finding the next benchmark when returns do not exist.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am facing the errors attached in the log file.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let nindexmin=5 /*I set arbitrary number greater than zero*/;

%macro findnextbenchmark;
%DO %WHILE (&amp;amp;nindex&amp;gt;0 and &amp;amp;nindexmin&amp;gt;0);
/*First time*/
data MinBench;
     set FirmsBench6;
      by cusip;
      /* Prevents LOWEST from being reset*/
      /* to missing for each iteration.   */
      retain minimum;
      /* Sets HIGHEST to missing for each */
      /* different ID value.              */
      If first.cusip then minimum=.;
      /* Compares lowest to diff of BMs in  */
      /* current iteration and resets     */
      /* value if Diff of BM is lower.        */
      minimum=min(minimum,Diff_BM);
      if first.cusip then output;
run;

proc sql; 
     create table temp as 
	 select *
	 from missing a
	 left join  MinBench b
	 on a.cusip=b.cusip;
quit;

proc sort data=temp out=fdf nodupkeys;
     by cusip;
run;

data temp(keep=CUSIP Conditional_Exercise_Date cusip_ben date_ipo retx_ipo countdays);
     set temp;
run;

proc sort data=temp;
     by cusip countdays;
run;

/*Updated*/
proc sql;
     create table FirmsBench6 as
	 select  a.*
	 from FirmsBench6 a
     left join  MinBench b
	 on a.cusip=b.cusip  
     where a.cusip_ben not in (select b.cusip_ben from MinBench);
quit;

proc sort data= FirmsBench6;
      by CUSIP ;
run;


%let psw=xxxxxxxx;
%let username=xxxxxxxx;

/* 	Push the macro variables to the remote server (without the next line the WRDS server 
	would not 'understand' &amp;amp;vars etc); */
%let wrds = wrds.wharton.upenn.edu 4016;options comamid = TCP remote=WRDS;
signon  noscript user=xxxxx'  password="&amp;amp;psw";

rsubmit;
libname crsplib '/wrds/crsp/sasdata/a_stock';
proc upload data=temp  out=work.temp ; run; 

/*Get returns after conditional exercise date*/
PROC SQL;
       create table temp_1 as
       select a.* , b.retx as retx_ben
       from work.temp  a 
       left join crsplib.dsf b
  	   on a.cusip_ben=b.cusip and a.date_ipo=b.date;
quit;

proc download data=temp_1 out=temp_1;
run;
endrsubmit;
signoff;


data temp_2/*(drop=retx_ben1)*/;
     set temp_1;
	 if retx_ben ne .;
     if retx_ben ne .B;
     if retx_ben ne .C;
	 *retx_ben1=retx_ben+0;
	 *if retx_ben1 ne . ;
run;

proc append base=styled_base data=temp_2;
run;

proc sort data=styled_base;
     by cusip date_ipo;
run;

data Missing(drop=cusip_ben retx_ben);
     set temp_1;
	 if retx_ben= . or retx_ben=.B or retx_ben=.C;
run;

proc sort data=Missing out=Missigben nodupkeys;
     by cusip;
run;

data _null_;
	call symput('nindex',put(obs,best.));
	stop;
	set Missigben nobs=obs;
run;


data _null_;
	call symput('nindexmin',put(obs,best.));
	stop;
	set WORK.FIRMSBENCH6 nobs=obs;
run;
*%if &amp;amp;nindexmin = 0 %then LEAVE; 

%put &amp;amp;nindex;
%put &amp;amp;nindexmin;
%END;
%mend;
%findnextbenchmark;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;File Error:&lt;/P&gt;
&lt;P&gt;91&lt;BR /&gt;20688&lt;BR /&gt;MPRINT(FINDNEXTBENCHMARK): * data MinBench;&lt;BR /&gt;NOTE: Line generated by the invoked macro "FINDNEXTBENCHMARK".&lt;BR /&gt;1054 data MinBench; set FirmsBench6;&lt;BR /&gt;---&lt;BR /&gt;180&lt;BR /&gt;MPRINT(FINDNEXTBENCHMARK): set FirmsBench6;&lt;/P&gt;
&lt;P&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/P&gt;
&lt;P&gt;NOTE: Line generated by the invoked macro "FINDNEXTBENCHMARK".&lt;BR /&gt;1054 by cusip;&lt;BR /&gt;--&lt;BR /&gt;180&lt;BR /&gt;MPRINT(FINDNEXTBENCHMARK): by cusip;&lt;/P&gt;
&lt;P&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/P&gt;
&lt;P&gt;NOTE: Line generated by the invoked macro "FINDNEXTBENCHMARK".&lt;BR /&gt;1054 retain minimum;&lt;BR /&gt;------&lt;BR /&gt;180&lt;BR /&gt;MPRINT(FINDNEXTBENCHMARK): retain minimum;&lt;/P&gt;
&lt;P&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/P&gt;
&lt;P&gt;NOTE: Line generated by the invoked macro "FINDNEXTBENCHMARK".&lt;BR /&gt;1054 If first.cusip then minimum=.;&lt;/P&gt;
&lt;P&gt;--&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;many thanks in advance&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2022 10:47:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-errors/m-p/818783#M323203</guid>
      <dc:creator>georgel</dc:creator>
      <dc:date>2022-06-17T10:47:18Z</dc:date>
    </item>
    <item>
      <title>Re: Macro errors</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-errors/m-p/818785#M323205</link>
      <description>Can you paste the contents of the log file into your question, or into a reply?  Many folks try to avoid downloading attachments from the internets.</description>
      <pubDate>Fri, 17 Jun 2022 10:43:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-errors/m-p/818785#M323205</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-06-17T10:43:20Z</dc:date>
    </item>
    <item>
      <title>Re: Macro errors</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-errors/m-p/818786#M323206</link>
      <description>&lt;P&gt;can you run the same code successfully outside the macro? and for debugging, it would be helpful if you turn on the options mprint mlogic symbolgen.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2022 10:48:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-errors/m-p/818786#M323206</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2022-06-17T10:48:42Z</dc:date>
    </item>
    <item>
      <title>Re: Macro errors</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-errors/m-p/818787#M323207</link>
      <description>&lt;P&gt;you can preview before downloading it but I agree with you that it's better to post it in the contents of the post itself.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2022 10:49:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-errors/m-p/818787#M323207</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2022-06-17T10:49:12Z</dc:date>
    </item>
    <item>
      <title>Re: Macro errors</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-errors/m-p/818790#M323210</link>
      <description>&lt;P&gt;Yes if I do it manually it works. the table FirmsBenchmark 6 is updated every time with fewer observations!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2022 10:54:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-errors/m-p/818790#M323210</guid>
      <dc:creator>georgel</dc:creator>
      <dc:date>2022-06-17T10:54:51Z</dc:date>
    </item>
    <item>
      <title>Re: Macro errors</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-errors/m-p/818791#M323211</link>
      <description>&lt;P&gt;can you post the log using &amp;lt;/&amp;gt; after running with MPRINT MLOGIC and SYMBOLGEN options?&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2022 10:59:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-errors/m-p/818791#M323211</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2022-06-17T10:59:30Z</dc:date>
    </item>
    <item>
      <title>Re: Macro errors</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-errors/m-p/818793#M323212</link>
      <description>&lt;P&gt;Note the asterisk in the code generated by your macro, it's commenting out the DATA statement. so the SET statement errors:&lt;/P&gt;
&lt;PRE&gt;MPRINT(FINDNEXTBENCHMARK):   * data MinBench;
NOTE: Line generated by the invoked macro "FINDNEXTBENCHMARK".
1054      data MinBench;      set FirmsBench6;
                              ---
                              180
MPRINT(FINDNEXTBENCHMARK):   set FirmsBench6;

&lt;/PRE&gt;
&lt;P&gt;That asterisk is not in your macro definition on the DATA statement.&amp;nbsp; So where is the * coming from?&amp;nbsp; Well, it's at the end of your macro:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*%if &amp;amp;nindexmin = 0 %then LEAVE; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That line is returning a * which you do not intend.&amp;nbsp; The * is not a comment in the macro language.&amp;nbsp; If you want that to be a macro comment you need to use %*, i.e.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%* if &amp;amp;nindexmin = 0 %then LEAVE; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or you could use /* */ :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
%if &amp;amp;nindexmin = 0 %then LEAVE;
*/ &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Jun 2022 11:04:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-errors/m-p/818793#M323212</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-06-17T11:04:41Z</dc:date>
    </item>
    <item>
      <title>Re: Macro errors</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-errors/m-p/818795#M323214</link>
      <description>&lt;P&gt;noticed that too! I like to comment code by highlighting it and pressing CTRL + shift.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2022 11:38:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-errors/m-p/818795#M323214</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2022-06-17T11:38:41Z</dc:date>
    </item>
    <item>
      <title>Re: Macro errors</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-errors/m-p/818796#M323215</link>
      <description>&lt;P&gt;Thank you so mush sir!&lt;/P&gt;
&lt;P&gt;I have removed the whole sentence and works. Yesterday it was working. This is the only thing I changed today!&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jun 2022 11:42:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-errors/m-p/818796#M323215</guid>
      <dc:creator>georgel</dc:creator>
      <dc:date>2022-06-17T11:42:38Z</dc:date>
    </item>
  </channel>
</rss>

