<?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: SAS Macros in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macros/m-p/405696#M279195</link>
    <description>&lt;P&gt;Where are &amp;amp;sdate1 and &amp;amp;sdate2 defined?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There looks to be a bunch of logical errors here. Those are just the ones I first saw.&lt;/P&gt;</description>
    <pubDate>Thu, 19 Oct 2017 19:26:53 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-10-19T19:26:53Z</dc:date>
    <item>
      <title>SAS Macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macros/m-p/405688#M279192</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using the following Macro code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%MACRO rollingreg&lt;BR /&gt;(data=mom12.developing, out_ds=results,&lt;BR /&gt;id=country, date=date,&lt;BR /&gt;model_equation= RETURN = MOM,&lt;BR /&gt;start_date= 12-31-1996, end_date= 12-31-2014,&lt;BR /&gt;freq=month, s=1, n=36,&lt;BR /&gt;regprint=noprint);&lt;/P&gt;&lt;P&gt;%* Start with empty output data sets;&lt;BR /&gt;proc datasets nolist;&lt;BR /&gt;delete _all_ds _outest_ds;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;* Prepare input data for by-id-date use;&lt;BR /&gt;proc sort data=&amp;amp;data;&lt;BR /&gt;by &amp;amp;id &amp;amp;date;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%* Set the 'by-id' variable;&lt;BR /&gt;%if &amp;amp;year_date=1 %then %let freq=year;&lt;BR /&gt;%* year frequency case;&lt;BR /&gt;%put Date variable: &amp;amp;date year_date: &amp;amp;year_date;&lt;BR /&gt;%put Start and end dates: &amp;amp;start_date &amp;amp;end_date // &amp;amp;sdate1 &amp;amp;sdate2;&lt;BR /&gt;%if &amp;amp;year_date=0 %then&lt;BR /&gt;%put %sysfunc(putn(&amp;amp;sdate1,date9.)) %sysfunc(putn(&amp;amp;sdate2,date9.));&lt;BR /&gt;%put Freq: &amp;amp;freq s: &amp;amp;s n: &amp;amp;n;&lt;/P&gt;&lt;P&gt;%* Preliminary date setting for each iteration/loop;&lt;BR /&gt;%* First end date (idate2) is n periods after the start date;&lt;BR /&gt;%if &amp;amp;year_date=1 %then %let idate2= %eval(&amp;amp;sdate1+(&amp;amp;n-1));&lt;BR /&gt;%else %let idate2= %sysfunc(intnx(&amp;amp;freq,&amp;amp;sdate1,(&amp;amp;n-1),end));&lt;BR /&gt;%if &amp;amp;year_date=0 %then %let idate1= %sysfunc(intnx(&amp;amp;freq,&amp;amp;idate2,-&amp;amp;n+1,begin));&lt;BR /&gt;%else %let idate1= %eval(&amp;amp;idate2-&amp;amp;n+1);&lt;BR /&gt;%put First loop: &amp;amp;idate1 -- &amp;amp;idate2;&lt;BR /&gt;%put Loop through: &amp;amp;sdate2;&lt;BR /&gt;%if (&amp;amp;idate2 &amp;gt; &amp;amp;sdate2) %then %do;&lt;/P&gt;&lt;P&gt;%* Dates are not acceptable-- show problem, do not run loop;&lt;BR /&gt;%put PROBLEM-- end date for loop exceeds range : ( &amp;amp;idate2 &amp;gt; &amp;amp;sdate2 );&lt;BR /&gt;%end;&lt;BR /&gt;%else %do; *Dates are accepted-- run loops;&lt;BR /&gt;%let jj=0;&lt;BR /&gt;%do %while(&amp;amp;idate2 &amp;lt;= &amp;amp;sdate2);&lt;BR /&gt;%let jj=%eval(&amp;amp;jj+1);&lt;/P&gt;&lt;P&gt;%*Define loop start date (idate1) based on inherited end date (idate2);&lt;BR /&gt;%if &amp;amp;year_date=0 %then %do;&lt;BR /&gt;%let idate1= %sysfunc(intnx(&amp;amp;freq,&amp;amp;idate2,-&amp;amp;n+1,begin));&lt;BR /&gt;%let date1c= %sysfunc(putn(&amp;amp;idate1,date9.));&lt;BR /&gt;%let date2c= %sysfunc(putn(&amp;amp;idate2,date9.));&lt;BR /&gt;%end;&lt;BR /&gt;%if &amp;amp;year_date=1 %then %do;&lt;BR /&gt;%let idate1= %eval(&amp;amp;idate2-&amp;amp;n+1);&lt;BR /&gt;%let date1c= &amp;amp;idate1;&lt;BR /&gt;%let date2c= &amp;amp;idate2;&lt;BR /&gt;%end;&lt;BR /&gt;%let idate1= %sysfunc(max(&amp;amp;sdate1,&amp;amp;idate1));&lt;BR /&gt;%put Loop: &amp;amp;jj -- &amp;amp;date1c &amp;amp;date2c;&lt;BR /&gt;%put &amp;amp;jj -- &amp;amp;idate1 &amp;amp;idate2;&lt;BR /&gt;proc datasets nolist;&lt;BR /&gt;delete _outest_ds;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%***** analysis code here -- for each loop;&lt;BR /&gt;%* noprint to just make output set;&lt;BR /&gt;%let noprint= noprint;&lt;BR /&gt;%if %upcase(®print) = yes | %upcase(®print) = print %then %let noprint= ;&lt;BR /&gt;proc reg data=&amp;amp;data&lt;BR /&gt;outest=_outest_ds edf&lt;BR /&gt;&amp;amp;noprint;&lt;BR /&gt;where &amp;amp;date between &amp;amp;idate1 and &amp;amp;idate2;&lt;BR /&gt;model &amp;amp;model_equation;&lt;BR /&gt;&amp;amp;by_id;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%* Add loop date range variables to output set;&lt;BR /&gt;data _outest_ds;&lt;BR /&gt;set _outest_ds;&lt;BR /&gt;regobs= _p_ + _edf_; %* number of observations in regression;&lt;BR /&gt;date1= &amp;amp;idate1;&lt;BR /&gt;date2= &amp;amp;idate2;&lt;BR /&gt;%if &amp;amp;year_date=0 %then format date1 date2 date9.;&lt;BR /&gt;run;&lt;BR /&gt;%* Append results;&lt;BR /&gt;proc datasets nolist;&lt;BR /&gt;append base=_all_ds data=_outest_ds;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%* Set next loop end date;&lt;BR /&gt;%if &amp;amp;year_date=0 %then %let idate2= %sysfunc(intnx(&amp;amp;freq,&amp;amp;idate2,&amp;amp;s,end));&lt;BR /&gt;%else %if &amp;amp;year_date=1 %then %let idate2= %eval(&amp;amp;idate2+&amp;amp;s);&lt;BR /&gt;%end; *% end of loop;&lt;BR /&gt;%* Save outout set to desired location;&lt;BR /&gt;data &amp;amp;out_ds;&lt;BR /&gt;set _all_ds;&lt;BR /&gt;run;&lt;BR /&gt;proc sort data=&amp;amp;out_ds;&lt;BR /&gt;by &amp;amp;id date2;&lt;BR /&gt;run;&lt;BR /&gt;%end; %* end for date check pass section;&lt;BR /&gt;%mend;&lt;BR /&gt;%rollingreg;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, whenever I run the above code, I see the following error message:&lt;/P&gt;&lt;P&gt;WARNING: Apparent symbolic reference YEAR_DATE not resolved.&lt;BR /&gt;ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric&lt;BR /&gt;operand is required. The condition was: &amp;amp;year_date=1&lt;BR /&gt;ERROR: The macro ROLLINGREG will stop executing.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2017 19:10:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macros/m-p/405688#M279192</guid>
      <dc:creator>Yamani</dc:creator>
      <dc:date>2017-10-19T19:10:58Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macros/m-p/405693#M279193</link>
      <description>&lt;P&gt;This is the first place that macro variable appears.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;year_date=1 %then %let freq=year;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It has not been set to a value before you used it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code would be easier to look at if you use the insert SAS code "run box"&amp;nbsp;when you paste it.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2017 19:15:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macros/m-p/405693#M279193</guid>
      <dc:creator>WarrenKuhfeld</dc:creator>
      <dc:date>2017-10-19T19:15:43Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macros/m-p/405695#M279194</link>
      <description>&lt;P&gt;Format the code so it's legible as well. The indents help you find errors and a wall of code is difficult to parse.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO rollingreg
			(data=mom12.developing, out_ds=results,
			id=country, date=date,
			model_equation= RETURN = MOM,
			start_date= 12-31-1996, end_date= 12-31-2014,
			freq=month, s=1, n=36,
			regprint=noprint);
	%* Start with empty output data sets;
	proc datasets nolist;
		delete _all_ds _outest_ds;
	run;

	* Prepare input data for by-id-date use;
	proc sort data=&amp;amp;data;
		by &amp;amp;id &amp;amp;date;
	run;

	%* Set the 'by-id' variable;
	%if &amp;amp;year_date=1 %then
		%let freq=year;

	%* year frequency case;
	%put Date variable: &amp;amp;date year_date: &amp;amp;year_date;
	%put Start and end dates: &amp;amp;start_date &amp;amp;end_date // &amp;amp;sdate1 &amp;amp;sdate2;

	%if &amp;amp;year_date=0 %then
		%put %sysfunc(putn(&amp;amp;sdate1,date9.)) %sysfunc(putn(&amp;amp;sdate2,date9.));
	%put Freq: &amp;amp;freq s: &amp;amp;s n: &amp;amp;n;

	%* Preliminary date setting for each iteration/loop;
	%* First end date (idate2) is n periods after the start date;
	%if &amp;amp;year_date=1 %then
		%let idate2= %eval(&amp;amp;sdate1+(&amp;amp;n-1));
	%else %let idate2= %sysfunc(intnx(&amp;amp;freq,&amp;amp;sdate1,(&amp;amp;n-1),end));

	%if &amp;amp;year_date=0 %then
		%let idate1= %sysfunc(intnx(&amp;amp;freq,&amp;amp;idate2,-&amp;amp;n+1,begin));
	%else %let idate1= %eval(&amp;amp;idate2-&amp;amp;n+1);
	%put First loop: &amp;amp;idate1 -- &amp;amp;idate2;
	%put Loop through: &amp;amp;sdate2;

	%if (&amp;amp;idate2 &amp;gt; &amp;amp;sdate2) %then
		%do;
			%* Dates are not acceptable-- show problem, do not run loop;
			%put PROBLEM-- end date for loop exceeds range : ( &amp;amp;idate2 &amp;gt; &amp;amp;sdate2 );
		%end;
	%else
		%do;
			*Dates are accepted-- run loops;
			%let jj=0;

			%do %while(&amp;amp;idate2 &amp;lt;= &amp;amp;sdate2);
				%let jj=%eval(&amp;amp;jj+1);

				%*Define loop start date (idate1) based on inherited end date (idate2);
				%if &amp;amp;year_date=0 %then
					%do;
						%let idate1= %sysfunc(intnx(&amp;amp;freq,&amp;amp;idate2,-&amp;amp;n+1,begin));
						%let date1c= %sysfunc(putn(&amp;amp;idate1,date9.));
						%let date2c= %sysfunc(putn(&amp;amp;idate2,date9.));
					%end;

				%if &amp;amp;year_date=1 %then
					%do;
						%let idate1= %eval(&amp;amp;idate2-&amp;amp;n+1);
						%let date1c= &amp;amp;idate1;
						%let date2c= &amp;amp;idate2;
					%end;

				%let idate1= %sysfunc(max(&amp;amp;sdate1,&amp;amp;idate1));
				%put Loop: &amp;amp;jj -- &amp;amp;date1c &amp;amp;date2c;
				%put &amp;amp;jj -- &amp;amp;idate1 &amp;amp;idate2;

				proc datasets nolist;
					delete _outest_ds;
				run;

				%***** analysis code here -- for each loop;
				%* noprint to just make output set;
				%let noprint= noprint;

				%if %upcase(®print) = yes | %upcase(®print) = print %then
					%let noprint=;

				proc reg data=&amp;amp;data
					outest=_outest_ds edf
					&amp;amp;noprint;
					where &amp;amp;date between &amp;amp;idate1 and &amp;amp;idate2;
					model &amp;amp;model_equation;
					&amp;amp;by_id;
				run;

				%* Add loop date range variables to output set;
				data _outest_ds;
					set _outest_ds;
					regobs= _p_ + _edf_;

					%* number of observations in regression;
					date1= &amp;amp;idate1;
					date2= &amp;amp;idate2;

					%if &amp;amp;year_date=0 %then
						format date1 date2 date9.;
				run;

				%* Append results;
				proc datasets nolist;
					append base=_all_ds data=_outest_ds;
				run;

				%* Set next loop end date;
				%if &amp;amp;year_date=0 %then
					%let idate2= %sysfunc(intnx(&amp;amp;freq,&amp;amp;idate2,&amp;amp;s,end));
				%else %if &amp;amp;year_date=1 %then
					%let idate2= %eval(&amp;amp;idate2+&amp;amp;s);
			%end;

			*% end of loop;
			%* Save outout set to desired location;
			data &amp;amp;out_ds;
				set _all_ds;
			run;

			proc sort data=&amp;amp;out_ds;
				by &amp;amp;id date2;
			run;

		%end;

	%* end for date check pass section;
%mend;

%rollingreg;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Oct 2017 19:23:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macros/m-p/405695#M279194</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-10-19T19:23:10Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macros/m-p/405696#M279195</link>
      <description>&lt;P&gt;Where are &amp;amp;sdate1 and &amp;amp;sdate2 defined?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There looks to be a bunch of logical errors here. Those are just the ones I first saw.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2017 19:26:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macros/m-p/405696#M279195</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-10-19T19:26:53Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macros/m-p/407041#M279196</link>
      <description>&lt;P&gt;Thanks for your help. Yes, you are correct.&amp;nbsp;The&amp;nbsp;&lt;SPAN&gt;macro variable year_date&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;has not been set to a value before you used it.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The year_date macro variable is used in the code ONLY IF I am using annual data. However, I am using monthly data not annual data, so how I can adjust the code for monthly data?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Oct 2017 19:17:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macros/m-p/407041#M279196</guid>
      <dc:creator>Yamani</dc:creator>
      <dc:date>2017-10-24T19:17:00Z</dc:date>
    </item>
  </channel>
</rss>

