<?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: Restructure Repeated Measures Data in Tall Format and Count Time Between Non-Missing Values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Restructure-Repeated-Measures-Data-in-Tall-Format-and-Count-Time/m-p/862132#M340536</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/440313"&gt;@Tallefield&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This works for your sample data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=_:);
do _i=1 by 1 until(last.id);
  set have;
  by id;
  if dv&amp;gt;. then do;
    _k=sum(_k,1);
    _t=lag(_i);
  end;
end;
if _k=1 then _t=.;
do _i=1 to _i;
  set have curobs=_c;
  if _i&amp;lt;=_t then do;
    if iv=. then output;
    else do;
      do Lag=1 by 1 until(dv&amp;gt;.);
        _p=_c+Lag;
        set have(keep=dv) point=_p;
      end;
      output;
      Lag=.;
    end;
  end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Maybe you can use it as a starting point for your real data.&lt;/P&gt;</description>
    <pubDate>Fri, 03 Mar 2023 14:54:36 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2023-03-03T14:54:36Z</dc:date>
    <item>
      <title>Restructure Repeated Measures Data in Tall Format and Count Time Between Non-Missing Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Restructure-Repeated-Measures-Data-in-Tall-Format-and-Count-Time/m-p/862030#M340482</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have repeated assessments that the number of non-missing values varies across participants. I need to a) restructure the data such that an IV aligns with a DV at the next observation that is not missing and b) create a new variable that counts the lag between observations.&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, if I start with data like this:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;IV&lt;/TD&gt;&lt;TD&gt;DV&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt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&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 02 Mar 2023 20:24:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Restructure-Repeated-Measures-Data-in-Tall-Format-and-Count-Time/m-p/862030#M340482</guid>
      <dc:creator>Tallefield</dc:creator>
      <dc:date>2023-03-02T20:24:20Z</dc:date>
    </item>
    <item>
      <title>Re: Restructure Repeated Measures Data in Tall Format and Count Time Between Non-Missing Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Restructure-Repeated-Measures-Data-in-Tall-Format-and-Count-Time/m-p/862046#M340488</link>
      <description>&lt;P&gt;Please be careful when using terms that are also SAS functions. You may not be aware that LAG is a SAS function that returns the value of variable from &lt;STRONG&gt;previous&lt;/STRONG&gt; observations. I really don't follow what you are requesting but it appears that your "Lag" is looking for something related to &lt;STRONG&gt;following&lt;/STRONG&gt; observations.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Mar 2023 22:08:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Restructure-Repeated-Measures-Data-in-Tall-Format-and-Count-Time/m-p/862046#M340488</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-03-02T22:08:39Z</dc:date>
    </item>
    <item>
      <title>Re: Restructure Repeated Measures Data in Tall Format and Count Time Between Non-Missing Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Restructure-Repeated-Measures-Data-in-Tall-Format-and-Count-Time/m-p/862124#M340530</link>
      <description>&lt;P&gt;Thanks for the advice. The code would need to count the timespan between the assessments - this is what I mean by lag.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Mar 2023 14:05:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Restructure-Repeated-Measures-Data-in-Tall-Format-and-Count-Time/m-p/862124#M340530</guid>
      <dc:creator>Tallefield</dc:creator>
      <dc:date>2023-03-03T14:05:34Z</dc:date>
    </item>
    <item>
      <title>Re: Restructure Repeated Measures Data in Tall Format and Count Time Between Non-Missing Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Restructure-Repeated-Measures-Data-in-Tall-Format-and-Count-Time/m-p/862132#M340536</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/440313"&gt;@Tallefield&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This works for your sample data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=_:);
do _i=1 by 1 until(last.id);
  set have;
  by id;
  if dv&amp;gt;. then do;
    _k=sum(_k,1);
    _t=lag(_i);
  end;
end;
if _k=1 then _t=.;
do _i=1 to _i;
  set have curobs=_c;
  if _i&amp;lt;=_t then do;
    if iv=. then output;
    else do;
      do Lag=1 by 1 until(dv&amp;gt;.);
        _p=_c+Lag;
        set have(keep=dv) point=_p;
      end;
      output;
      Lag=.;
    end;
  end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Maybe you can use it as a starting point for your real data.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Mar 2023 14:54:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Restructure-Repeated-Measures-Data-in-Tall-Format-and-Count-Time/m-p/862132#M340536</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-03-03T14:54:36Z</dc:date>
    </item>
    <item>
      <title>Re: Restructure Repeated Measures Data in Tall Format and Count Time Between Non-Missing Values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Restructure-Repeated-Measures-Data-in-Tall-Format-and-Count-Time/m-p/862179#M340551</link>
      <description>&lt;P&gt;Thank you so much! Also works great in the real data. Thanks again!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Mar 2023 17:52:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Restructure-Repeated-Measures-Data-in-Tall-Format-and-Count-Time/m-p/862179#M340551</guid>
      <dc:creator>Tallefield</dc:creator>
      <dc:date>2023-03-03T17:52:26Z</dc:date>
    </item>
  </channel>
</rss>

