<?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: create a loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/create-a-loop/m-p/270531#M53775</link>
    <description>&lt;P&gt;Thank you so much Tom! It works! &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt; My final code is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro test;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;%do i=1 %to 48;&lt;BR /&gt;&lt;BR /&gt;proc glm data=GLS.Sample;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;model _1_&amp;amp;i. = _2_&amp;amp;i. _3_&amp;amp;i. _4_&amp;amp;i;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;ods output ParameterEstimates=GLS.Coeff&amp;amp;i;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;proc append base=GLS.All_Coeff data=GLS.Coeff&amp;amp;i. force;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc delete data=GLS.Coeff&amp;amp;i;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;%end;&lt;BR /&gt;%mend test;&lt;BR /&gt;%test(runs=48);&lt;/P&gt;</description>
    <pubDate>Sat, 14 May 2016 18:10:40 GMT</pubDate>
    <dc:creator>Alain38</dc:creator>
    <dc:date>2016-05-14T18:10:40Z</dc:date>
    <item>
      <title>create a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-loop/m-p/270517#M53766</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;I'm new to SAS and I would like to create a loop from 1 to 48. Column _1_i is projected onto&lt;BR /&gt;columns _2_i, _3_i and _4_i using Generalized Least Squares method. So there are 48 regressions to perform.&lt;BR /&gt;Coefficients (Estimate) and standard-errors (StdErr) are extracted in a dataset called "Coeff".&lt;BR /&gt;I wrote an example, without loop, with just the 2 first regressions:&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; proc glm data=Sample;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; model _1_1 = _2_1 _3_1 _4_1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ods output ParameterEstimates=Coeff;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; quit;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; proc glm data=Sample;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; model _1_2 = _2_2 _3_2 _4_2;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ods output ParameterEstimates=Coeff2;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; quit;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Add new obs to original data set */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; proc append base=Coeff data=Coeff2;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; run;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Delete the temp data set */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; proc delete data= Coeff2;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; run;&lt;BR /&gt;&lt;BR /&gt;I tried to create a macro with "%do i=1 %to 48" and "model &amp;amp;_1_i. = &amp;amp;_2_i. &amp;amp;_3_i. &amp;amp;_4_i.;" but I don't manage to make it work, notably because of the temporary datasets supposed to be named Coeff2, ..., to Coeff48. I use them to add results in the existing dataset "Coeff", and then delete them (only way I found to add new results without overwriting).&lt;BR /&gt;&lt;BR /&gt;Thank you in advance so much for your help,&lt;/P&gt;</description>
      <pubDate>Sat, 14 May 2016 14:58:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-loop/m-p/270517#M53766</guid>
      <dc:creator>Alain38</dc:creator>
      <dc:date>2016-05-14T14:58:02Z</dc:date>
    </item>
    <item>
      <title>Re: create a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-loop/m-p/270519#M53767</link>
      <description>&lt;P&gt;Looks like you are not quite getting the syntax for how to use the value of the macro variable to generate names. &amp;nbsp;Essentially SAS will just replace the reference to the macro variable with text that it contains.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if you want generate the variable name pattern of _1_1, _1_2, _1_3, ... &amp;nbsp;then you use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;_1_&amp;amp;i&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might also need to watch out if the variable part is in the middle of a string. &amp;nbsp;In that case you need to add a period after the name of the macro variable so that SAS can tell where the name ends and the normal text begins again.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;_var&amp;amp;i._with_suffix&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 14 May 2016 15:28:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-loop/m-p/270519#M53767</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-05-14T15:28:50Z</dc:date>
    </item>
    <item>
      <title>Re: create a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-loop/m-p/270523#M53771</link>
      <description>&lt;P&gt;Thank you so much for your quick reply. Is the syntax the same to name the output datasets? i.e.:&lt;/P&gt;&lt;P&gt;"ods output ParameterEstimates=Coeff&amp;amp;i"&lt;/P&gt;&lt;P&gt;since I will have 48 output datasets (Coeff, Coeff2, ..., Coeff48), including 47 which are temporary and that I will delete after merging with the first one.&lt;/P&gt;</description>
      <pubDate>Sat, 14 May 2016 16:04:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-loop/m-p/270523#M53771</guid>
      <dc:creator>Alain38</dc:creator>
      <dc:date>2016-05-14T16:04:31Z</dc:date>
    </item>
    <item>
      <title>Re: create a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-loop/m-p/270526#M53772</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;run it with N=2 or 3 and test.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general if want to repeat a step that generates a data set and combine them you could use proc append. &amp;nbsp;You would want to do something like this in the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;.... out=coeff&amp;amp;i ....

proc append base=all_coeff data=coeff&amp;amp;i force ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then before you call the macro you might add this to make sure that you don't have an old version of your base dataset lying around.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc delete data=all_coeff;
run;

%mymacro(runs=48);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that using PROC APPEND requires that the datasets have the same variables. &amp;nbsp;If the datasets might have different variables you might need to use a data step instead so that you can incorporate new variables as they are generated.&lt;/P&gt;</description>
      <pubDate>Sat, 14 May 2016 17:08:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-loop/m-p/270526#M53772</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-05-14T17:08:11Z</dc:date>
    </item>
    <item>
      <title>Re: create a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-loop/m-p/270531#M53775</link>
      <description>&lt;P&gt;Thank you so much Tom! It works! &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt; My final code is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro test;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;%do i=1 %to 48;&lt;BR /&gt;&lt;BR /&gt;proc glm data=GLS.Sample;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;model _1_&amp;amp;i. = _2_&amp;amp;i. _3_&amp;amp;i. _4_&amp;amp;i;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;ods output ParameterEstimates=GLS.Coeff&amp;amp;i;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;proc append base=GLS.All_Coeff data=GLS.Coeff&amp;amp;i. force;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc delete data=GLS.Coeff&amp;amp;i;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;%end;&lt;BR /&gt;%mend test;&lt;BR /&gt;%test(runs=48);&lt;/P&gt;</description>
      <pubDate>Sat, 14 May 2016 18:10:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-loop/m-p/270531#M53775</guid>
      <dc:creator>Alain38</dc:creator>
      <dc:date>2016-05-14T18:10:40Z</dc:date>
    </item>
  </channel>
</rss>

