<?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: Counting observations within groups of observations that fulfill specific criteria in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Counting-observations-within-groups-of-observations-that-fulfill/m-p/600420#M18317</link>
    <description>&lt;P&gt;Hi Mark,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your reply. Yes, you're correct in your summary.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If possible, I would like a data similar to what you describe, which includes for each individual subject (I have 60 more records such as the one I sent as an example with a first column ID which identifies the individual subjects), the start and end record of each REM group, and the number of NREM and WAKE that intervene. I need to ultimately be able to quantify the number of intervening NREM and WAKE records for statistical analysis later per subject. The more information I could get from the data the better, e.g., which REM groups tend to have more intervening NREM and WAKE records, those at the beginning of the data set or at the end. If I could have a way of answering that question with a new data set then that would be excellent. If that complicates things then forget it. It's no problem. Please let me know if you have any questions if you wish to continue further assisting me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sincerely,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ian&lt;/P&gt;</description>
    <pubDate>Wed, 30 Oct 2019 14:57:12 GMT</pubDate>
    <dc:creator>USCSS_Nostromo</dc:creator>
    <dc:date>2019-10-30T14:57:12Z</dc:date>
    <item>
      <title>Counting observations within groups of observations that fulfill specific criteria</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Counting-observations-within-groups-of-observations-that-fulfill/m-p/600061#M18300</link>
      <description>&lt;P&gt;Dear SAS communities,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am interested in counting observations within groups of observations in sets of human sleep data that fulfill specific criteria.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the course of a night's sleep, an individual goes through various stages of sleep, which can be categorized as rapid eye movement (REM) sleep or non-rapid eye movement sleep (NREM) or occasionally wakefulness (WAKE). These stages typically occur in groups. I wish to count the number of instances (observations) of NREM sleep and WAKE which occur during groups of REM sleep only. If I could get a column with this information then that would be fine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One exception: Should there be more than 15 observations of either NREM or WAKE within a group of REM sleep, then this would not be counted.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you help me?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data set is attached.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have two columns in the data set: ID and Type. The data (REM, NREM and WAKE) are in the Type column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please feel free to ask me anything.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am running SAS 9.4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sincerely,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ian&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, 29 Oct 2019 13:39:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Counting-observations-within-groups-of-observations-that-fulfill/m-p/600061#M18300</guid>
      <dc:creator>USCSS_Nostromo</dc:creator>
      <dc:date>2019-10-29T13:39:04Z</dc:date>
    </item>
    <item>
      <title>Re: Counting observations within groups of observations that fulfill specific criteria</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Counting-observations-within-groups-of-observations-that-fulfill/m-p/600109#M18303</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/228331"&gt;@USCSS_Nostromo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Dear SAS communities,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am interested in counting observations within groups of observations in sets of human sleep data that fulfill specific criteria.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the course of a night's sleep, an individual goes through various stages of sleep, which can be categorized as rapid eye movement (REM) sleep or non-rapid eye movement sleep (NREM) or occasionally wakefulness (WAKE). These stages typically occur in groups. I wish to count the number of instances (observations) of NREM sleep and WAKE which occur during groups of REM sleep only. If I could get a column with this information then that would be fine.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One exception: Should there be more than 15 observations of either NREM or WAKE within a group of REM sleep, then this would not be counted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have two columns in the data set: ID and Type. The data (REM, NREM and WAKE) are in the Type column.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is your definition of "groups of REM sleep"? &amp;nbsp;&amp;nbsp; Does the sequence of&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; REM/WAKE/WAKE/WAKE/WAKE/….(20 WAKEs)/REM&lt;/P&gt;
&lt;P&gt;constitute a group of REM sleep?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is yes, then does REM/WAKE/WAKE/REM/WAKE/WAKE/WAKE/WAKE/WAKE/REM&amp;nbsp; constitute one REM sleep group or two?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 16:16:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Counting-observations-within-groups-of-observations-that-fulfill/m-p/600109#M18303</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-10-29T16:16:31Z</dc:date>
    </item>
    <item>
      <title>Re: Counting observations within groups of observations that fulfill specific criteria</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Counting-observations-within-groups-of-observations-that-fulfill/m-p/600353#M18314</link>
      <description>&lt;P&gt;Hello mkeintz,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The definition of a REM sleep group is a sequence of REM that is &lt;U&gt;&amp;gt;&lt;/U&gt; 15 instances or more or REM. There can be intervening NREM or Wake without ending the REM sleep group as long as the &lt;U&gt;contiguous&lt;/U&gt; intervening NREM or WAKE together are &amp;lt; 15 in number.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is one exception to the above rules, however: the first group of REM sleep in the data can be any length of REM instances. It does not have to be &lt;U&gt;&amp;gt;&lt;/U&gt; 15 in number. Here are some examples.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are per data set that I have 4-5 REM sleep groups. The overarching aim is to see how fragmented a given individual's REM sleep is per night. The instances of WAKE and NREM within each REM sleep group count as disruptions of the continuity of REM sleep. The more the total number of these disruptions is, the more fragmented an individual's REM sleep is.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Examples:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This example counts as a REM sleep group because there are &lt;U&gt;&amp;gt;&lt;/U&gt; 15 REM instances with &amp;lt; 15 instances of NREM or WAKE intervening:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;REM/REM/REM/REM/REM/REM/REM/REM/REM/REM/REM/REM/REM/WAKE/WAKE/NREM/REM/REM/REM/REM/REM/REM&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This example does not count as a REM sleep group because there are &amp;lt;15 REM instances:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;REM/REM/REM/REM/REM/REM/REM/REM/REM/WAKE/WAKE/NREM/REM/REM/REM/NREM/NREM&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This example does not count as a REM sleep group because there are &lt;U&gt;&amp;gt;&lt;/U&gt; 15 instances of either contiguous NREM or WAKE intervening. If there are &lt;U&gt;&amp;gt;&lt;/U&gt; 15 instances of NREM or WAKE intervening but they are not contiguous, then they do not invalidate the REM sleep group:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;REM/REM/REM/REM/REM/REM/REM/REM/REM/REM/REM/REM/WAKE/WAKE/WAKE/WAKE/WAKE/WAKE/WAKE/WAKE/WAKE/WAKE/WAKE/NREM/NREM/NREM/NREM/REM/REM/REM/REM/REM/REM&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This example counts as a REM sleep group EVEN THOUGH it is &amp;lt; 15 instances of REM in number because it is the first REM sleep encountered in the night:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;REM/REM/REM/WAKE/WAKE/NREM/REM/REM/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope that this helps. Please let me know if you have any other questions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sincerely,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ian&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2019 12:18:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Counting-observations-within-groups-of-observations-that-fulfill/m-p/600353#M18314</guid>
      <dc:creator>USCSS_Nostromo</dc:creator>
      <dc:date>2019-10-30T12:18:49Z</dc:date>
    </item>
    <item>
      <title>Re: Counting observations within groups of observations that fulfill specific criteria</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Counting-observations-within-groups-of-observations-that-fulfill/m-p/600413#M18316</link>
      <description>&lt;P&gt;So a REM group must&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&amp;nbsp;Begin and end with a REM record.&lt;/LI&gt;
&lt;LI&gt;&amp;nbsp;Contain at least 15 REM records (except the first REM group which could have as few as two REM records&lt;/LI&gt;
&lt;LI&gt;&amp;nbsp;Contain no gap of non-REM records of size 15 or larger&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The basic strategy is that you are using gaps of 15 or more to separate REM groups. But some stray REM records will be part of no REM group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So what do you want the output to look like?&amp;nbsp; It could be the same 1200 records you presented with a REM group ID variable added. Or it could be a data set (or report) just identifies, for each group, the starting record, end record, number of REM, number of NREM, and number of WAKE records.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or perhaps you have something else in mind.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards,&lt;/P&gt;
&lt;P&gt;Mark&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2019 14:35:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Counting-observations-within-groups-of-observations-that-fulfill/m-p/600413#M18316</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-10-30T14:35:12Z</dc:date>
    </item>
    <item>
      <title>Re: Counting observations within groups of observations that fulfill specific criteria</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Counting-observations-within-groups-of-observations-that-fulfill/m-p/600420#M18317</link>
      <description>&lt;P&gt;Hi Mark,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your reply. Yes, you're correct in your summary.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If possible, I would like a data similar to what you describe, which includes for each individual subject (I have 60 more records such as the one I sent as an example with a first column ID which identifies the individual subjects), the start and end record of each REM group, and the number of NREM and WAKE that intervene. I need to ultimately be able to quantify the number of intervening NREM and WAKE records for statistical analysis later per subject. The more information I could get from the data the better, e.g., which REM groups tend to have more intervening NREM and WAKE records, those at the beginning of the data set or at the end. If I could have a way of answering that question with a new data set then that would be excellent. If that complicates things then forget it. It's no problem. Please let me know if you have any questions if you wish to continue further assisting me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sincerely,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ian&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2019 14:57:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Counting-observations-within-groups-of-observations-that-fulfill/m-p/600420#M18317</guid>
      <dc:creator>USCSS_Nostromo</dc:creator>
      <dc:date>2019-10-30T14:57:12Z</dc:date>
    </item>
    <item>
      <title>Re: Counting observations within groups of observations that fulfill specific criteria</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Counting-observations-within-groups-of-observations-that-fulfill/m-p/600546#M18323</link>
      <description>&lt;P&gt;I think the logic of this program is to read the data in batches by ID TYPE, generating the size of each run of each TYPE, call it a RUNSIZE. &amp;nbsp;&amp;nbsp; Keep generating sequential RUNSIZE values (updating the total REM, NREM, and WAKE record counts at the same time), until you encounter a total of trailing runsize for NREM and WAKE greater the 15.&amp;nbsp; (A "trailing" count is just the number of the records types since the end of the last run of REM records:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile 'c:\temp\t.txt' truncover dlm='	';  /* using download of your data here*/
  recid=_n_;
  input ID :$6.  TYPE :$4. ;
run;


data want (keep=id group_id rem_beg rem_end rem_beg_r rem_end_r N_rem n_nrem n_wake);

  label GROUP_ID   = 'Group number (within ID) for this REM group'
        REM_BEG    = 'Record ID for first REM record in this group'
        REM_END    = 'Record ID for last REM record in this group'
        REM_BEG_R  = 'Relative record id within an ID for REM_BEG'
        REM_END_R  = 'Relative record id within an ID for REM_END'
        REM_END_R  = 'Within ID value for REM_BEG'
        N_REM      = 'Number of REM records in this group'
        N_NREM     = 'Number of NREM records in this group'
        N_WAKE     = 'Number of WAKE records in this group'    ;

  retain END_OF_PRIOR_ID 0;                  /* Track the ending RID (see below) for preceding ID */
  /* Iterate below until end of ID or a gap (TRAILING_NREM + TRAILING_WAKE) is large enough */
  do until (sum(trailing_nrem,trailing_wake,0)&amp;gt;15 or last.id=1); 
    do runsize=1 by 1 until (last.type);     /* Find runsize for the current TYPE */
      set have;
      by id  type notsorted;
      rid+1;                                 /* Track Record identifier */
    end;

    if type='REM' then do;                           /* If this was a run of REM records ...   */
      rem_end=rid;
      if rem_beg=. then do;                          /* If REM group just starting ...         */
        rem_beg= rid - runsize + 1;                  /*   ... initial REM_BEG ...              */
        n_nrem=0;                                    /*   ... and N_NREM ...                   */
        n_wake=0;                                    /*   ... and N_WAKE ...                   */
      end;

      trailing_nrem=0;                               /* Reset trailing NREM run size           */
      trailing_wake=0;
      N_rem+runsize;                                 /* Update total count of REM records      */
    end;
    else if type='NREM' then do;
      trailing_nrem+runsize;                         /* Size of all trailing NREM runs */
      N_NREM+runsize;
    end;
    else if type='WAKE' then do;
      trailing_wake+runsize;                         /* Size of all trailing WAKE runs */
      N_WAKE+runsize;
    end;
  end; 

  /* Now that a large gap or end-of-id has been encountered ... */
  if N_rem&amp;gt;= ifn(group_id&amp;gt;=1,15,2) then do;  /* If this is a qualifying REM group ...          */
    N_nrem = N_nrem - trailing_nrem;         /* ... Subtract count of trailing NREM records    */
    N_wake = N_wake - trailing_wake;         /* ... Same with any trailing WAKE records        */
    REM_BEG_R= rem_beg-end_of_prior_id;      /* ... Get the relative REM_BEG ...               */
    REM_END_R= rem_end-end_of_prior_id;      /* ... Get the relative REM_END ...               */
    group_id+1;                              /* ... Update the group identifier                */
    output;
  end;
  call missing(of rem_:, of N_:, of trailing_:);
  if last.id then do;
     call missing(group_id);
     end_of_prior_id=rid;
  end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So the outer loop is a do until a large trailing gapsize (NREM and WAKE records) exceed 15 or end of an ID group.&lt;/P&gt;
&lt;P&gt;The inside loop reads records in batches, by type.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once the gap or end-of-id is encountered, just inspect the N_REM (&amp;gt;=15 except for the first group), and subtract the number of trailing nrem and wake records.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The expression&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ifn(group_id&amp;gt;=1,15,2)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Returns a 15 when group_id&amp;gt;=1&amp;nbsp; and returns a 2 otherwise.&amp;nbsp; This is a handy way to set different count requirements for the first rem group vs all the remaining rem groups. &amp;nbsp; Since group_id is increment only AFTER qualification is established, this is a handy way to implement differing limits.&amp;nbsp; Note at the end of each id, the group_id variable is reset to missing, so that the next ID is properly initialized.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Oct 2019 00:38:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Counting-observations-within-groups-of-observations-that-fulfill/m-p/600546#M18323</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-10-31T00:38:05Z</dc:date>
    </item>
    <item>
      <title>Re: Counting observations within groups of observations that fulfill specific criteria</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Counting-observations-within-groups-of-observations-that-fulfill/m-p/601319#M18340</link>
      <description>&lt;P&gt;Dear Mark,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again for your help with this. Your code works perfectly, even when I include additional data sets from other samples.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sincerely,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ian&lt;/P&gt;</description>
      <pubDate>Mon, 04 Nov 2019 09:04:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Counting-observations-within-groups-of-observations-that-fulfill/m-p/601319#M18340</guid>
      <dc:creator>USCSS_Nostromo</dc:creator>
      <dc:date>2019-11-04T09:04:01Z</dc:date>
    </item>
  </channel>
</rss>

