<?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 which would update a variable's value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-which-would-update-a-variable-s-value/m-p/548038#M151933</link>
    <description>&lt;P&gt;If I understand the question correctly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data miss2;
   set miss1;
   array idata {5} miss1-miss5;
   array dd {5} d1-d5;
   d1=0;
   unatt_count=0;
   do i = 2 to dim(idata);
      unatt_count + (idata{i-1} = 0);
      if idata{i}=0 then dd{i} = unatt_count;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;D1 must be 0, since there can't be any unattended meetings before meeting 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;MISS5 never gets counted in the unattended count, since it can't be used as a prior meeting.&amp;nbsp; If you want to adjust the final unattended count to examine MISS5 you could add a final statement to the DATA step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if miss5=0 then unatt_count + 1;&lt;/P&gt;</description>
    <pubDate>Tue, 02 Apr 2019 20:16:10 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2019-04-02T20:16:10Z</dc:date>
    <item>
      <title>Do loop which would update a variable's value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-which-would-update-a-variable-s-value/m-p/547995#M151913</link>
      <description>&lt;P&gt;Hi SAS programmers,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am writing this do loop, but cannot really wrap my head around how to make SAS update unatt_count within a loop.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So say we have a dataset&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data miss;&lt;BR /&gt;input miss1-miss5;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 1 1 1&lt;BR /&gt;1 0 1 1 1&lt;BR /&gt;1 1 0 0 1&lt;BR /&gt;1 0 0 0 1&lt;BR /&gt;1 0 0 0 0&lt;BR /&gt;1 0 0 1 1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Miss1-miss5 take a value of 1 if a subject attended the visit, and 0 if the subject did not attend.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to calculate variables d1-d5 for each miss1-miss5 so that for each attended visit the relative "d_" would count the number of unattended visit until the attended visit.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So far what I wrote is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data miss2;&lt;BR /&gt;set miss1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; unatt_count = 0;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; array idata(*) miss1-miss5;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; array dd(*) d1-d5;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;do i = 1 to dim(idata);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;if idata(i) = 1&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;then dd(i) = unatt_count ;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;else if idata(i) = 0&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;then dd(i) = unatt_count+1;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As expected SAS only returns values 1 and 0 for d1-d5, because unatt_count needs to be updated within a loop. I know my loop may be completely idiotic for the purpose (I am a novice to SAS programming), so I would really appreciate any advice!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2019 18:07:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-which-would-update-a-variable-s-value/m-p/547995#M151913</guid>
      <dc:creator>Dinurik</dc:creator>
      <dc:date>2019-04-02T18:07:03Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop which would update a variable's value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-which-would-update-a-variable-s-value/m-p/547998#M151914</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/267481"&gt;@Dinurik&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi SAS programmers,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am writing this do loop, but cannot really wrap my head around how to make SAS update unatt_count within a loop.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So say we have a dataset&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data miss;&lt;BR /&gt;input miss1-miss5;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 1 1 1&lt;BR /&gt;1 0 1 1 1&lt;BR /&gt;1 1 0 0 1&lt;BR /&gt;1 0 0 0 1&lt;BR /&gt;1 0 0 0 0&lt;BR /&gt;1 0 0 1 1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Miss1-miss5 take a value of 1 if a subject attended the visit, and 0 if the subject did not attend.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to calculate variables d1-d5 for each miss1-miss5 so that for each attended visit the relative "d_" would count the number of unattended visit until the attended visit.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Show us the desired output from this sample data.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2019 18:10:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-which-would-update-a-variable-s-value/m-p/547998#M151914</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-04-02T18:10:23Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop which would update a variable's value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-which-would-update-a-variable-s-value/m-p/548011#M151917</link>
      <description>&lt;P&gt;I think you're really looking for the first 1 in the series?&lt;/P&gt;
&lt;P&gt;If so, WHICHN is all you need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data miss;
input miss1-miss5;

first1 = whichn(1, of miss1-miss5);

cards;
1 1 1 1 1
1 0 1 1 1
1 1 0 0 1
1 0 0 0 1
1 0 0 0 0
1 0 0 1 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/267481"&gt;@Dinurik&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi SAS programmers,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am writing this do loop, but cannot really wrap my head around how to make SAS update unatt_count within a loop.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So say we have a dataset&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data miss;&lt;BR /&gt;input miss1-miss5;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 1 1 1&lt;BR /&gt;1 0 1 1 1&lt;BR /&gt;1 1 0 0 1&lt;BR /&gt;1 0 0 0 1&lt;BR /&gt;1 0 0 0 0&lt;BR /&gt;1 0 0 1 1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Miss1-miss5 take a value of 1 if a subject attended the visit, and 0 if the subject did not attend.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to calculate variables d1-d5 for each miss1-miss5 so that for each attended visit the relative "d_" would count the number of unattended visit until the attended visit.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So far what I wrote is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data miss2;&lt;BR /&gt;set miss1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; unatt_count = 0;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; array idata(*) miss1-miss5;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; array dd(*) d1-d5;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;do i = 1 to dim(idata);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;if idata(i) = 1&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;then dd(i) = unatt_count ;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;else if idata(i) = 0&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;then dd(i) = unatt_count+1;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As expected SAS only returns values 1 and 0 for d1-d5, because unatt_count needs to be updated within a loop. I know my loop may be completely idiotic for the purpose (I am a novice to SAS programming), so I would really appreciate any advice!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2019 19:02:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-which-would-update-a-variable-s-value/m-p/548011#M151917</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-04-02T19:02:13Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop which would update a variable's value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-which-would-update-a-variable-s-value/m-p/548038#M151933</link>
      <description>&lt;P&gt;If I understand the question correctly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data miss2;
   set miss1;
   array idata {5} miss1-miss5;
   array dd {5} d1-d5;
   d1=0;
   unatt_count=0;
   do i = 2 to dim(idata);
      unatt_count + (idata{i-1} = 0);
      if idata{i}=0 then dd{i} = unatt_count;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;D1 must be 0, since there can't be any unattended meetings before meeting 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;MISS5 never gets counted in the unattended count, since it can't be used as a prior meeting.&amp;nbsp; If you want to adjust the final unattended count to examine MISS5 you could add a final statement to the DATA step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if miss5=0 then unatt_count + 1;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2019 20:16:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-which-would-update-a-variable-s-value/m-p/548038#M151933</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-04-02T20:16:10Z</dc:date>
    </item>
  </channel>
</rss>

