<?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: Looping with Variable Names in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365129#M275102</link>
    <description>&lt;P&gt;thanks....if macro usage is not recommended, is there another way to accomplish this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-Bill&lt;/P&gt;</description>
    <pubDate>Wed, 07 Jun 2017 18:26:11 GMT</pubDate>
    <dc:creator>BCNAV</dc:creator>
    <dc:date>2017-06-07T18:26:11Z</dc:date>
    <item>
      <title>Looping with Variable Names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365111#M275099</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am coming over from EViews so bear with me. I would like to run a loop over variables names that are then used as a forecast variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what I have. It doesn't work, but it will give you the idea of what I am trying to do (there could be many airports in this example, I have chosen just two here).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;&lt;P&gt;-Bill&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro forecast_hw;&lt;/P&gt;&lt;P&gt;%let airport1 = "CYVR";&lt;BR /&gt;%let airport2 = "CYYJ";&lt;/P&gt;&lt;P&gt;%do i = &amp;amp;airport1 %to &amp;amp;airport2;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; proc forecast data=EGTASK.AIRPORT_TS interval=month trend=2 alpha=0.05 method=winters seasons=month lead=12 nstart=max nsstart=max out=FCST_Winters_Auto_&amp;amp;i outest=Est_Winters_Auto_&amp;amp;i;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; id date;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var &amp;amp;i.TCU8;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where date &amp;lt;= '01Apr2017'd; run;&lt;/P&gt;&lt;P&gt;%end;&lt;BR /&gt;%mend;&lt;BR /&gt;%forecast_hw;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here&amp;nbsp;are the &amp;nbsp;errors...sorry for getting them:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:&lt;/P&gt;&lt;P&gt;&amp;amp;airport1&lt;/P&gt;&lt;P&gt;ERROR: The %FROM value of the %DO I loop is invalid.&lt;/P&gt;&lt;P&gt;ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:&lt;/P&gt;&lt;P&gt;&amp;amp;airport2&lt;/P&gt;&lt;P&gt;ERROR: The %TO value of the %DO I loop is invalid.&lt;/P&gt;&lt;P&gt;ERROR: The macro FORECAST_HW will stop executing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 17:45:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365111#M275099</guid>
      <dc:creator>BCNAV</dc:creator>
      <dc:date>2017-06-07T17:45:34Z</dc:date>
    </item>
    <item>
      <title>Re: Looping with Variable Names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365112#M275100</link>
      <description>&lt;P&gt;Give us the slightest chance of helping you ... what doesn't work? What is the error message? Turn on the MPRINT option, run it again and show us the SASLOG.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 17:43:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365112#M275100</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-06-07T17:43:04Z</dc:date>
    </item>
    <item>
      <title>Re: Looping with Variable Names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365120#M275101</link>
      <description>&lt;P&gt;Starting out with macro language (when you're not an experienced SAS programmer) is a daunting task, not usually recommended.&amp;nbsp; But assuming that the SAS code within your PROC works, here are some issues to attend to:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Macro language does not use quotes around the values of macro variables&lt;/LI&gt;
&lt;LI&gt;Macro language %DO loops are limited, and can iterate over a range of numeric values only&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Here's one way you might structure your macro:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro forecast_hw (airport_list=);&lt;/P&gt;
&lt;P&gt;%local n i;&lt;/P&gt;
&lt;P&gt;%do n = 1 %to %sysfunc(countw(&amp;amp;airport_list));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %let i = %scan(&amp;amp;airport_list, &amp;amp;n);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; proc forecast data=EGTASK.AIRPORT_TS interval=month trend=2 alpha=0.05 method=winters seasons=month lead=12 nstart=max nsstart=max out=FCST_Winters_Auto_&amp;amp;i outest=Est_Winters_Auto_&amp;amp;i;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; id date;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var &amp;amp;i.TCU8;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where date &amp;lt;= '01Apr2017'd; run;&lt;/P&gt;
&lt;P&gt;%end;&lt;BR /&gt;%mend;&lt;BR /&gt;%forecast_hw (airport_list=CYVR CYYJ)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In practice, you might want to change &amp;amp;i to some other, more meaningful name.&amp;nbsp; I left it that way only because I was too lazy to change it every place that it appears in the code.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 18:03:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365120#M275101</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-06-07T18:03:44Z</dc:date>
    </item>
    <item>
      <title>Re: Looping with Variable Names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365129#M275102</link>
      <description>&lt;P&gt;thanks....if macro usage is not recommended, is there another way to accomplish this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-Bill&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 18:26:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365129#M275102</guid>
      <dc:creator>BCNAV</dc:creator>
      <dc:date>2017-06-07T18:26:11Z</dc:date>
    </item>
    <item>
      <title>Re: Looping with Variable Names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365131#M275103</link>
      <description>&lt;P&gt;to do this in PROC FORECAST without a macro, you could use the BY statement and then the analysis will execute for each airport.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 18:32:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365131#M275103</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-06-07T18:32:03Z</dc:date>
    </item>
    <item>
      <title>Re: Looping with Variable Names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365132#M275104</link>
      <description>&lt;P&gt;Macro language is often the right tool for the job.&amp;nbsp; In this case, it might be best.&amp;nbsp; It's just that you have to learn two things at the same time when you are just starting out (SAS language and macro language).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another approach would require that you structure your data differently.&amp;nbsp; You have different variable names for each of the airports.&amp;nbsp; If you were to create a narrower data set (separate observations for each airport) and add AIRPORT as a variable, you could process your data BY AIRPORT, with the same variable each time.&amp;nbsp; There could conceivably be just a single PROC, with no macro language.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 18:31:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365132#M275104</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-06-07T18:31:40Z</dc:date>
    </item>
    <item>
      <title>Re: Looping with Variable Names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365153#M275105</link>
      <description>&lt;P&gt;Rules for writing a macro - first start with working BASE SAS code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So once you have that code working, you can then start adding macro variables one at a time and testing it to make sure it works. Then you can post components so we can help you debug any issues.&lt;/P&gt;
&lt;P&gt;What does your Base code look like and show several variables so we can understand how you want to loop this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Others are correct, in that there are multiple ways to do things. One common 'trick' is to reformat the data so you can use BY processing and skip macro's entirely.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 19:07:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365153#M275105</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-06-07T19:07:53Z</dc:date>
    </item>
    <item>
      <title>Re: Looping with Variable Names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365171#M275106</link>
      <description>&lt;P&gt;Macro %DO loops only index over integer values&amp;nbsp;- i.e.&amp;nbsp;&amp;nbsp; %do I=1 %to 10;&amp;nbsp;&amp;nbsp; or %do I= &amp;amp;x %to &amp;amp;y (if &amp;amp;x and &amp;amp;y are integers).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now, in your case you could do this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; %let airport_list= CYVR CYYJ ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %do W=1 %to %sysfunc(countw(&amp;amp;airport_list));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %let I=%scan(&amp;amp;airport_list,&amp;amp;W,%str());&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ... the rest here ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you can also do something not quite as opaque as %macro.&amp;nbsp; You can use an ordinary&amp;nbsp;do loop inside a DATA _NULL_ step.&amp;nbsp; Using the PUT statement, write&amp;nbsp;desired statements out to a temporary file, and then %include that file to run the statements.&amp;nbsp; A lot less macro learning, yet same flexibility for your problem.&amp;nbsp; In fact, more flexibility since you can loop over character values (as in&amp;nbsp;"DO airport="CYVR","CYYR").&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename tmp temporary;

data _null_;
  file tmp;   /* Identify target for PUT statements */
  do airport="CYVR","CYYR";
     put "proc forecast data=egtask_airport_ts inverval=month trend=2 alpha=0.05"&lt;BR /&gt;       / "&amp;nbsp;method=winters" easons=month lead=12 nstart=max nsstart=max ";&lt;BR /&gt;
     txt=cats("out=FCST_Winters_Auto_",airport);    put txt;
     txt=cats("outest=Est_Winters_Auto_",airport,';'); put txt;
     put 'id date';
     txt=cats(airport,"TCU8"); put "var " txt;
     put "where date &amp;lt;= '01Apr2017'd; run;";
  end;
run;

options source2;    /* Tell SAS to print out the INCLUDED statements */
%include tmp;   /* Read and execute contents of TMP */


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note the slash in&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; PUT "some text"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /&amp;nbsp; "some more txt" ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;tells sas to advance one line between "some text" and "some more text".&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 20:22:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365171#M275106</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-06-07T20:22:38Z</dc:date>
    </item>
    <item>
      <title>Re: Looping with Variable Names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365188#M275107</link>
      <description>&lt;P&gt;Thanks for the macro help. Believe it or not, the macro usage is the easiest for me to understand, and I was able to explain it to colleagues as it is intuitive. So thanks! A bit more cumbersome than EViews for this type of step, but still fun.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It would take more effort to split out the data as the time series database has monthly data on airport activity and charges as separate variables for each airport (e.g., Month CYOW_Activity, CYOW_Charges, CYVR_Activity, CYVR_Charges, etc.). Not sure how it could be broken up easily to use the "by" command in proc forecast. If there are ideas for this let me know; always willing to learn.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks to all for your assistance!&lt;/P&gt;&lt;P&gt;-Bill&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 22:39:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365188#M275107</guid>
      <dc:creator>BCNAV</dc:creator>
      <dc:date>2017-06-07T22:39:30Z</dc:date>
    </item>
    <item>
      <title>Re: Looping with Variable Names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365208#M275108</link>
      <description>&lt;P&gt;Explanation:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-run-multiple-regressions/ta-p/223663" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-run-multiple-regressions/ta-p/223663&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/Macro-for-multiple-GARCH-Regressions/m-p/184279/highlight/true#M46872" target="_blank"&gt;https://communities.sas.com/t5/SAS-Procedures/Macro-for-multiple-GARCH-Regressions/m-p/184279/highlight/true#M46872&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jun 2017 00:59:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365208#M275108</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-06-08T00:59:08Z</dc:date>
    </item>
    <item>
      <title>Re: Looping with Variable Names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365412#M275109</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/142314"&gt;@BCNAV&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for the macro help. Believe it or not, the macro usage is the easiest for me to understand, and I was able to explain it to colleagues as it is intuitive. So thanks! A bit more cumbersome than EViews for this type of step, but still fun.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It would take more effort to split out the data as the time series database has monthly data on airport activity and charges as separate variables for each airport (e.g., Month CYOW_Activity, CYOW_Charges, CYVR_Activity, CYVR_Charges, etc.). Not sure how it could be broken up easily to use the "by" command in proc forecast. If there are ideas for this let me know; always willing to learn.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks to all for your assistance!&lt;/P&gt;
&lt;P&gt;-Bill&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If CYOW CYVR are airports then the transform to data with a structure like:&lt;/P&gt;
&lt;P&gt;Month Airport Activity Charges&lt;/P&gt;
&lt;P&gt;would allow use of BY AIRPORT processing after sorting by Airport (and likey Month). This is often referred to transforming from Wide to Long format and you will find dozens if not hundreds of examples on this website in one form or another.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jun 2017 15:21:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Variable-Names/m-p/365412#M275109</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-06-08T15:21:04Z</dc:date>
    </item>
  </channel>
</rss>

