<?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 Using do loops to code consecutive events in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-do-loops-to-code-consecutive-events/m-p/595892#M171502</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am having trouble with something that appears pretty simple to do. I have administrative data on individual patients identified by ID, date of event (year-month-day) and the two types of events, A and B. What I want to do is to figure out if event B tends to follow event A. I sorted the data by ID and date, and then I tried to use a simple do loop. I don't really know how to use loops all that well, but my understanding is that I had to create an array first.:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;indics = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&amp;nbsp;&amp;nbsp; *indicator variable&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;array&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; ed {&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;} A;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;array&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; psych {&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;} B;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;do&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; a = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;to&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1181047&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; ed[a] = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt; &amp;amp; psych[a+&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;] = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; indics = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;end&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;I get an error saying that index is out of bounds. I tried using the number of non-missing values as the total number of iterations, but it still didn't work. I am wondering if this approach can work in theory, or should I try something else?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;thank you&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 11 Oct 2019 21:13:18 GMT</pubDate>
    <dc:creator>AbuYusuf</dc:creator>
    <dc:date>2019-10-11T21:13:18Z</dc:date>
    <item>
      <title>Using do loops to code consecutive events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-do-loops-to-code-consecutive-events/m-p/595892#M171502</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am having trouble with something that appears pretty simple to do. I have administrative data on individual patients identified by ID, date of event (year-month-day) and the two types of events, A and B. What I want to do is to figure out if event B tends to follow event A. I sorted the data by ID and date, and then I tried to use a simple do loop. I don't really know how to use loops all that well, but my understanding is that I had to create an array first.:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;indics = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&amp;nbsp;&amp;nbsp; *indicator variable&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;array&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; ed {&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;} A;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;array&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; psych {&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;} B;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;do&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; a = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;to&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1181047&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; ed[a] = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt; &amp;amp; psych[a+&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;] = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; indics = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;end&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;I get an error saying that index is out of bounds. I tried using the number of non-missing values as the total number of iterations, but it still didn't work. I am wondering if this approach can work in theory, or should I try something else?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;thank you&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2019 21:13:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-do-loops-to-code-consecutive-events/m-p/595892#M171502</guid>
      <dc:creator>AbuYusuf</dc:creator>
      <dc:date>2019-10-11T21:13:18Z</dc:date>
    </item>
    <item>
      <title>Re: Using do loops to code consecutive events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-do-loops-to-code-consecutive-events/m-p/595907#M171511</link>
      <description>&lt;P&gt;Please post some sample data in a DATA step. This type of logic can be easily solved without arrays.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2019 22:04:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-do-loops-to-code-consecutive-events/m-p/595907#M171511</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2019-10-11T22:04:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using do loops to code consecutive events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-do-loops-to-code-consecutive-events/m-p/595908#M171512</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An array is intended to treat a collections of variables within a single observation - i.e. typically within a "row" of data.&amp;nbsp; But your description of the data suggests you have multiple observations ( 1 per event) for each individual.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please show a sample, in the form of a SAS data set with actual data, of what your dataset looks like, and what you would want the analysis result to look like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2019 22:07:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-do-loops-to-code-consecutive-events/m-p/595908#M171512</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-10-11T22:07:45Z</dc:date>
    </item>
    <item>
      <title>Re: Using do loops to code consecutive events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-do-loops-to-code-consecutive-events/m-p/595918#M171515</link>
      <description>&lt;P&gt;Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also what gets done when two sequential events are the same?&lt;/P&gt;
&lt;P&gt;Are there ever more than 2 records? How would that effect the output?&lt;/P&gt;
&lt;P&gt;You really should show what you expect the indicator to actually look like for given results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if you have data as separate records per event then a variable should indicate which type of record is involved. Something like this:&lt;/P&gt;
&lt;PRE&gt;data example;
   input id date :date9. event $;
   format date date9.;
datalines;
1 02Feb1966 A
1 14Mar1967 B
2 18Jun1988 B
2 23Aug1989 A
2 18Sep1989 A
2 18Oct1989 B
;

proc sort data=example;
  by id date;
run;

data want;
  set example;
  by id;
  levent = lag(event);
  if not first.id and event ne levent then eventsequence = catx('&amp;gt;',levent, event);
  drop levent;
run; 

&lt;/PRE&gt;
&lt;P&gt;The above also shows an example of how to post a data set as data step code.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2019 23:04:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-do-loops-to-code-consecutive-events/m-p/595918#M171515</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-10-11T23:04:12Z</dc:date>
    </item>
    <item>
      <title>Re: Using do loops to code consecutive events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-do-loops-to-code-consecutive-events/m-p/596539#M171771</link>
      <description>&lt;P&gt;Thank you very much for your response. Will post some sample data in&amp;nbsp;a response below.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Oct 2019 14:26:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-do-loops-to-code-consecutive-events/m-p/596539#M171771</guid>
      <dc:creator>AbuYusuf</dc:creator>
      <dc:date>2019-10-15T14:26:24Z</dc:date>
    </item>
    <item>
      <title>Re: Using do loops to code consecutive events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-do-loops-to-code-consecutive-events/m-p/596541#M171772</link>
      <description>&lt;P&gt;Thank you for your help! I will post some sample data below. But you are right, I have administrative hospital data on patients moving between hospitals. There are unique IDs, but there may be multiple observations per individual. I am interested if Event2&amp;nbsp; follows Event1. I'd like to create a dichotomous variable that equals 1 if event2 follows event1 and 0 otherwise.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Oct 2019 14:29:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-do-loops-to-code-consecutive-events/m-p/596541#M171772</guid>
      <dc:creator>AbuYusuf</dc:creator>
      <dc:date>2019-10-15T14:29:44Z</dc:date>
    </item>
    <item>
      <title>Re: Using do loops to code consecutive events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-do-loops-to-code-consecutive-events/m-p/596546#M171774</link>
      <description>&lt;P&gt;Thank you very much for your response and the code. Here is sample data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data WORK.SAMPLEDATA;&lt;/P&gt;
&lt;P&gt;infile datalines dsd truncover;&lt;/P&gt;
&lt;P&gt;input ID:BEST12. Date:$13. Event1:BEST12. Event2:BEST12.;&lt;/P&gt;
&lt;P&gt;format ID BEST12. Event1 BEST12. Event2 BEST12.;&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;1 19/Oct/2018 0 0&lt;/P&gt;
&lt;P&gt;1 01/Dec/2018 1 1&lt;/P&gt;
&lt;P&gt;1 12/Jan/2018 0 1&lt;/P&gt;
&lt;P&gt;2 01/Sep/2018 0 1&lt;/P&gt;
&lt;P&gt;2 05/Nov/2018 1 1&lt;/P&gt;
&lt;P&gt;2 03/Feb/2018 0 0&lt;/P&gt;
&lt;P&gt;2 01/Mar/2018 0 1&lt;/P&gt;
&lt;P&gt;2 19/Sep/2018 0 0&lt;/P&gt;
&lt;P&gt;3 08/Sep/2018 0 0&lt;/P&gt;
&lt;P&gt;3 04/Apr/2018 0 1&lt;/P&gt;
&lt;P&gt;3 14/Apr/2018 1 1&lt;/P&gt;
&lt;P&gt;4 20/Apr/2018 1 1&lt;/P&gt;
&lt;P&gt;4 04/Jun/2018 0 0&lt;/P&gt;
&lt;P&gt;5 11/Jun/2018 0 1&lt;/P&gt;
&lt;P&gt;5 21/Jun/2018 0 0&lt;/P&gt;
&lt;P&gt;;;;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think the code you provided is very close to what I'd like to do. It's just I have 2 variables instead of 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Oct 2019 14:48:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-do-loops-to-code-consecutive-events/m-p/596546#M171774</guid>
      <dc:creator>AbuYusuf</dc:creator>
      <dc:date>2019-10-15T14:48:42Z</dc:date>
    </item>
  </channel>
</rss>

