<?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 how to do a loop(do) in SAS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-do-a-loop-do-in-SAS/m-p/129750#M26487</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I want to do a loop to do regression multiple times. Let me briefly talk about my case. I have a dataset with variables id,y, x1, x2, x3, ... x10 and t. id is stock id, y is return, x1...x10 are some factors, t is time. First I want to do a time series regression of y on x1 for each id (should have t months) to get coefficients of x1, then regress some change of y on the coefficients of x1 for each t (month). I need to repeat this for 10 times, x1 until x10. I wrote a program to do one rolling regression, but want to use a do loop to repeat from x1 to x10. I attach part of my code here. Please help me loop over x1 to x10 part. Thanks.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;* Define independent and dependent variables;&lt;/P&gt;&lt;P&gt;%let x1=def;&lt;/P&gt;&lt;P&gt;%let x2=smb;&lt;/P&gt;&lt;P&gt;%let x3=hml;&lt;/P&gt;&lt;P&gt;%let x4=mom;&lt;/P&gt;&lt;P&gt;%let x5=def;&lt;/P&gt;&lt;P&gt;%let x6=term;&lt;/P&gt;&lt;P&gt;%let x7=div;&lt;/P&gt;&lt;P&gt;%let x8=inf;&lt;/P&gt;&lt;P&gt;%let x9=ip;&lt;/P&gt;&lt;P&gt;%let x10=pyrl;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let y=ret;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data betas1; stop; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro tsreg; * time series regression;&lt;/P&gt;&lt;P&gt;%do m = 37 %to 216; * from month 37 to 216;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; data roll; set temp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if &amp;amp;m - 36 &amp;lt;= t &amp;lt; &amp;amp;m; * Past 36 months data, for example, for month 37, t=1-36;&lt;/P&gt;&lt;P&gt;&amp;nbsp; mon = &amp;amp;m; * Create a variable "mon" for each m loop;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; proc reg data=roll noprint outest=results EDF; &lt;/P&gt;&lt;P&gt;&amp;nbsp; model &amp;amp;y=&amp;amp;x1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by productreference mon; &lt;/P&gt;&lt;P&gt;&amp;nbsp; run;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&amp;nbsp; data betas1 ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; set betas1 results;&lt;/P&gt;&lt;P&gt;&amp;nbsp; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;%mend tsreg;&lt;/P&gt;&lt;P&gt;%tsreg&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data = betas1;&lt;/P&gt;&lt;P&gt;by t;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc reg data = betas1 noprint outest = final;&lt;/P&gt;&lt;P&gt;model &amp;amp;y = &amp;amp;x1;&lt;/P&gt;&lt;P&gt;by t;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 09 Jul 2013 19:24:19 GMT</pubDate>
    <dc:creator>SeanZ</dc:creator>
    <dc:date>2013-07-09T19:24:19Z</dc:date>
    <item>
      <title>how to do a loop(do) in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-do-a-loop-do-in-SAS/m-p/129750#M26487</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I want to do a loop to do regression multiple times. Let me briefly talk about my case. I have a dataset with variables id,y, x1, x2, x3, ... x10 and t. id is stock id, y is return, x1...x10 are some factors, t is time. First I want to do a time series regression of y on x1 for each id (should have t months) to get coefficients of x1, then regress some change of y on the coefficients of x1 for each t (month). I need to repeat this for 10 times, x1 until x10. I wrote a program to do one rolling regression, but want to use a do loop to repeat from x1 to x10. I attach part of my code here. Please help me loop over x1 to x10 part. Thanks.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;* Define independent and dependent variables;&lt;/P&gt;&lt;P&gt;%let x1=def;&lt;/P&gt;&lt;P&gt;%let x2=smb;&lt;/P&gt;&lt;P&gt;%let x3=hml;&lt;/P&gt;&lt;P&gt;%let x4=mom;&lt;/P&gt;&lt;P&gt;%let x5=def;&lt;/P&gt;&lt;P&gt;%let x6=term;&lt;/P&gt;&lt;P&gt;%let x7=div;&lt;/P&gt;&lt;P&gt;%let x8=inf;&lt;/P&gt;&lt;P&gt;%let x9=ip;&lt;/P&gt;&lt;P&gt;%let x10=pyrl;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let y=ret;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data betas1; stop; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro tsreg; * time series regression;&lt;/P&gt;&lt;P&gt;%do m = 37 %to 216; * from month 37 to 216;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; data roll; set temp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if &amp;amp;m - 36 &amp;lt;= t &amp;lt; &amp;amp;m; * Past 36 months data, for example, for month 37, t=1-36;&lt;/P&gt;&lt;P&gt;&amp;nbsp; mon = &amp;amp;m; * Create a variable "mon" for each m loop;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; proc reg data=roll noprint outest=results EDF; &lt;/P&gt;&lt;P&gt;&amp;nbsp; model &amp;amp;y=&amp;amp;x1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by productreference mon; &lt;/P&gt;&lt;P&gt;&amp;nbsp; run;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&amp;nbsp; data betas1 ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; set betas1 results;&lt;/P&gt;&lt;P&gt;&amp;nbsp; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;%mend tsreg;&lt;/P&gt;&lt;P&gt;%tsreg&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data = betas1;&lt;/P&gt;&lt;P&gt;by t;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc reg data = betas1 noprint outest = final;&lt;/P&gt;&lt;P&gt;model &amp;amp;y = &amp;amp;x1;&lt;/P&gt;&lt;P&gt;by t;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 09 Jul 2013 19:24:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-do-a-loop-do-in-SAS/m-p/129750#M26487</guid>
      <dc:creator>SeanZ</dc:creator>
      <dc:date>2013-07-09T19:24:19Z</dc:date>
    </item>
    <item>
      <title>Re: how to do a loop(do) in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-do-a-loop-do-in-SAS/m-p/129751#M26488</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Here's a perfect example of what you want to do:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A class="active_link" href="http://www.ats.ucla.edu/stat/sas/seminars/sas_macros_introduction/" title="http://www.ats.ucla.edu/stat/sas/seminars/sas_macros_introduction/"&gt;http://www.ats.ucla.edu/stat/sas/seminars/sas_macros_introduction/&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 09 Jul 2013 19:50:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-do-a-loop-do-in-SAS/m-p/129751#M26488</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2013-07-09T19:50:07Z</dc:date>
    </item>
    <item>
      <title>Re: how to do a loop(do) in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-do-a-loop-do-in-SAS/m-p/129752#M26489</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Do you have missing values in these variables? If not, you don't need more macro loops. The REG procedure supports multiple MODEL statements.&amp;nbsp; Just list all of the explanatory variables on the VAR statement and then use multiple MODEL statements for your 10 variables within a single PROC REG call.&amp;nbsp; Here's an example:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc reg data=sashelp.cars noprint outest=results;&lt;/P&gt;&lt;P&gt;var Cylinders EngineSize Length MPG_City Weight;&lt;/P&gt;&lt;P&gt;Cylinders:&amp;nbsp; model Horsepower = Cylinders;&lt;/P&gt;&lt;P&gt;EngineSize: model Horsepower = EngineSize;&lt;/P&gt;&lt;P&gt;Length:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; model Horsepower = Length;&lt;/P&gt;&lt;P&gt;MPG_City:&amp;nbsp;&amp;nbsp; model Horsepower = MPG_City;&lt;/P&gt;&lt;P&gt;Weight:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; model Horsepower = Weight;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Look in the OUTEST= data set and you'll see the parameter estimates for each model.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Sep 2014 18:22:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-do-a-loop-do-in-SAS/m-p/129752#M26489</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2014-09-03T18:22:14Z</dc:date>
    </item>
    <item>
      <title>Re: how to do a loop(do) in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-do-a-loop-do-in-SAS/m-p/432945#M107277</link>
      <description>Hi Rick, referring to the above example, I have a similar situation to deal with. Just that I have more than 50 vars in the dataset and all of them are categorical.&lt;BR /&gt;&lt;BR /&gt;I need to use one independent variable and the dependent variable in each iteration and I think leveraging macros to automate this step would be the thing to do here. If so, can you please help me as to how I should go about it? I’m having a hard time to do this. Would appreciate your thoughts. Thanks. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 31 Jan 2018 23:36:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-do-a-loop-do-in-SAS/m-p/432945#M107277</guid>
      <dc:creator>Shashank7</dc:creator>
      <dc:date>2018-01-31T23:36:58Z</dc:date>
    </item>
    <item>
      <title>Re: how to do a loop(do) in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-do-a-loop-do-in-SAS/m-p/433105#M107350</link>
      <description>&lt;P&gt;See the article &lt;A href="https://blogs.sas.com/content/iml/2017/02/13/run-1000-regressions.html" target="_self"&gt;"An easy way to run thousands of regressions in SAS,"&lt;/A&gt;&amp;nbsp;which compares the macro loop and a BY group analysis. If you have categorical variables you will need to use PROC GLM and include a CLASS statement. If you run into trouble, start a new thread and post example data and code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Feb 2018 13:58:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-do-a-loop-do-in-SAS/m-p/433105#M107350</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2018-02-01T13:58:08Z</dc:date>
    </item>
  </channel>
</rss>

