<?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: Trouble with do loop in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Trouble-with-do-loop/m-p/243063#M55890</link>
    <description>&lt;P&gt;Please provide an example of the desired output. Also include at least one example where V1 is used.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A key part is likely to be the function LAG which allows values on previous records in a dataset to be examined at the same time as the current row.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Lv1= Lag(v1);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Lv2 = Lag(v2);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if Lv2 in (86, 0) then output;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
    <pubDate>Tue, 12 Jan 2016 21:06:08 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2016-01-12T21:06:08Z</dc:date>
    <item>
      <title>Trouble with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Trouble-with-do-loop/m-p/243058#M55888</link>
      <description>&lt;P&gt;Hi, I'm a SAS novice so sorry if this is obvious.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to find the first instance of a certain value for a variable. For example, this is some of my data&amp;amp;colon;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id grp mon v1 v2&lt;BR /&gt;1 1 0 0 86&lt;BR /&gt;1 1 1 0 86&lt;BR /&gt;1 1 2 0 23&lt;BR /&gt;1 1 3 0 23&lt;BR /&gt;1 1 4 0 23&lt;BR /&gt;2 3 0 86 86&lt;BR /&gt;2 3 1 86 16&lt;BR /&gt;2 3 2&amp;nbsp;86 16&lt;BR /&gt;2 3 3&amp;nbsp;16 20&lt;BR /&gt;2 3 4&amp;nbsp;16 20&lt;BR /&gt;3 2 0 0 86&lt;BR /&gt;3 2 1 0 86&lt;BR /&gt;3 2 2 0 86&lt;BR /&gt;3 2 3 0 16&lt;BR /&gt;3 2 4 0 20&lt;BR /&gt;4 3 0 1 86&lt;BR /&gt;4 3 1 3 28&lt;BR /&gt;4 3 2 2 28&lt;BR /&gt;4 3 3 2 28&lt;BR /&gt;4 3 4 2 24&lt;BR /&gt;5 3 0 2 86&lt;BR /&gt;5 3 1 3 16&lt;BR /&gt;5 3 2 4 24&lt;BR /&gt;5 3 3 4 24&lt;BR /&gt;5 3 4 3 16&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="line-height: 20px;"&gt;The data set has the participants id, which group they were in, month data was collected, and two variables of interest, v1 and v2. For v1 and v2, I would like to be able to pull out the first value after 0 or 86. For example, for id &lt;/SPAN&gt;&lt;SPAN style="line-height: 20px;"&gt;1&lt;/SPAN&gt;&lt;SPAN style="line-height: 20px;"&gt;, the value of v2 I am interested in is 2&lt;/SPAN&gt;&lt;SPAN style="line-height: 20px;"&gt;3&lt;/SPAN&gt;&lt;SPAN style="line-height: 20px;"&gt;; for id &lt;/SPAN&gt;&lt;SPAN style="line-height: 20px;"&gt;4&lt;/SPAN&gt;&lt;SPAN style="line-height: 20px;"&gt;&amp;nbsp;it is 2&lt;/SPAN&gt;&lt;SPAN style="line-height: 20px;"&gt;8&lt;/SPAN&gt;&lt;SPAN style="line-height: 20px;"&gt;, etc.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I thought a round about way to do it would might be:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data check; set check;&lt;BR /&gt;if v1=86 then miss=.;&lt;BR /&gt;else if v1=0 then miss=.;&lt;BR /&gt;else do;&lt;BR /&gt;miss +1;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then I could filter for miss=1 and find my values of interest. The problem is that I do not know how to automatically repeat the code for each id.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be appreciated! Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jan 2016 20:55:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Trouble-with-do-loop/m-p/243058#M55888</guid>
      <dc:creator>rkhachey</dc:creator>
      <dc:date>2016-01-12T20:55:59Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Trouble-with-do-loop/m-p/243059#M55889</link>
      <description>&lt;P&gt;BY group processing - it's one of the best features of SAS programming.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming this works for your cases, and that the data is sorted by ID this should help you get started. SAS will identify that it's a new ID and then you can reset or initialize any variables need.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data check;
set check;
BY ID;

if first.ID then do;
&amp;nbsp;*set variables to missing or reset as necessary;
end;

if v1=86 then miss=.;
else if v1=0 then miss=.;
else do;
miss +1;


output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jan 2016 21:01:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Trouble-with-do-loop/m-p/243059#M55889</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-12T21:01:53Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Trouble-with-do-loop/m-p/243063#M55890</link>
      <description>&lt;P&gt;Please provide an example of the desired output. Also include at least one example where V1 is used.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A key part is likely to be the function LAG which allows values on previous records in a dataset to be examined at the same time as the current row.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Lv1= Lag(v1);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Lv2 = Lag(v2);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if Lv2 in (86, 0) then output;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jan 2016 21:06:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Trouble-with-do-loop/m-p/243063#M55890</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-01-12T21:06:08Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Trouble-with-do-loop/m-p/243101#M55891</link>
      <description>&lt;P&gt;Yes. Bring up the output you need.&lt;/P&gt;
&lt;P&gt;Assuming ID variable has been sorted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id grp mon v1 v2;
cards;
1 1 0 0 86
1 1 1 0 86
1 1 2 0 23
1 1 3 0 23
1 1 4 0 23
2 3 0 86 86
2 3 1 86 16
2 3 2 86 16
2 3 3 16 20
2 3 4 16 20
3 2 0 0 86
3 2 1 0 86
3 2 2 0 86
3 2 3 0 16
3 2 4 0 20
4 3 0 1 86
4 3 1 3 28
4 3 2 2 28
4 3 3 2 28
4 3 4 2 24
5 3 0 2 86
5 3 1 3 16
5 3 2 4 24
5 3 3 4 24
5 3 4 3 16
;
run;
data want;
 set have;
 if id eq  lag(id) and v2 ne 86 and lag(v2)=86;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Jan 2016 01:15:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Trouble-with-do-loop/m-p/243101#M55891</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-01-13T01:15:59Z</dc:date>
    </item>
  </channel>
</rss>

