<?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: Do LOOP based on the value of a variable? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-LOOP-based-on-the-value-of-a-variable/m-p/644806#M192665</link>
    <description>&lt;P&gt;What is your desired result here?&lt;/P&gt;</description>
    <pubDate>Sun, 03 May 2020 09:29:24 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2020-05-03T09:29:24Z</dc:date>
    <item>
      <title>Do LOOP based on the value of a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-LOOP-based-on-the-value-of-a-variable/m-p/644805#M192664</link>
      <description>&lt;PRE&gt;data temp;
infile DATALINES dsd missover;
input ID DATE(month_num) num_obs var1;
CARDS;
01, 1, 5, .
01, 2, 5, 1
01, 3, 5, 1
01, 4, 5, 2
01, 5, 5, 2
02, 1, 4, .
02, 2, 4, 1
02, 3, 4, 2
02, 4, 4, 3
02, 5, 4, 4
;
run;&lt;/PRE&gt;&lt;P&gt;I'm trying to do a do-loop that varies in the last iteration, for each id, based on the value of 'num_obs' for that ID.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I want something like&lt;/P&gt;&lt;P&gt;DO i = 1 to num_obs;&lt;/P&gt;&lt;P&gt;&amp;lt;bla bla bla&amp;gt;&lt;/P&gt;&lt;P&gt;END;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So that my DO loop evaluates from i = 1 to 5 for ID = 1,&lt;/P&gt;&lt;P&gt;then from i = 1 to 4 for ID = 2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is this possible?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also need to use this in a macro, because for each loop, I'll be using i has a macro variable like so:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%DO i = 1 %to num_obs;

IF lag&amp;amp;i(var) = bla bla bla;

%END;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Is this possible?&lt;BR /&gt;&lt;BR /&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Sun, 03 May 2020 09:26:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-LOOP-based-on-the-value-of-a-variable/m-p/644805#M192664</guid>
      <dc:creator>UniversitySas</dc:creator>
      <dc:date>2020-05-03T09:26:10Z</dc:date>
    </item>
    <item>
      <title>Re: Do LOOP based on the value of a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-LOOP-based-on-the-value-of-a-variable/m-p/644806#M192665</link>
      <description>&lt;P&gt;What is your desired result here?&lt;/P&gt;</description>
      <pubDate>Sun, 03 May 2020 09:29:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-LOOP-based-on-the-value-of-a-variable/m-p/644806#M192665</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-05-03T09:29:24Z</dc:date>
    </item>
    <item>
      <title>Re: Do LOOP based on the value of a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-LOOP-based-on-the-value-of-a-variable/m-p/644808#M192666</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/95638"&gt;@UniversitySas&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can definitely&amp;nbsp;&lt;SPAN&gt;do a do-loop that varies in the last iteration, for each id, according to a variable.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Please have a look at the following result -&amp;gt; I have not dropped i so that you can see the number of iterations -&amp;gt;ex, for ID=01,&amp;nbsp;&lt;/SPAN&gt;numobs = 5, i looped from 1 to 5. When i=6, the do loop stops as 6 &amp;gt; numobs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set temp;
	do i = 1 to num_obs;
	end;
run;

proc print;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture d’écran 2020-05-03 à 11.35.34.png" style="width: 281px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/39008iCAD4577EC2EF2D60/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture d’écran 2020-05-03 à 11.35.34.png" alt="Capture d’écran 2020-05-03 à 11.35.34.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regarding your second question, you cannot write this&amp;nbsp; %DO i = 1 %TO numobs;&lt;/P&gt;
&lt;P&gt;-&amp;gt; numobs should be a macrovariable.&lt;/P&gt;
&lt;P&gt;Could you please give some more details about what you're going to achieve?&lt;/P&gt;
&lt;P&gt;NB: another thing is that using the LAG function in an IF statement is not recommended at all, leading to unexpected results. You should fix the lagged values in a variables before using them in a conditional statement.&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
      <pubDate>Sun, 03 May 2020 10:03:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-LOOP-based-on-the-value-of-a-variable/m-p/644808#M192666</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-05-03T10:03:12Z</dc:date>
    </item>
  </channel>
</rss>

