<?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: Retain value based on the condition per ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retain-value-based-on-the-condition-per-ID/m-p/637348#M189454</link>
    <description>&lt;P&gt;Here is one way for the given data.&lt;/P&gt;
&lt;PRE&gt; data have;
    input id visit cvd;
datalines;&lt;BR /&gt;1   1    0
1   3    0
1   5    1
1   9    0
2   3    .
2  4    1
2  5    0
2   9    .
;


data want;
   set have;
   by id;
   retain cvdtru;
   /* reset the retained value for the first visit
      of each id
   */
   if first.id then call missing(cvdtru);
   /* when find the cvd=1 then set the retained value*/
   if cvd=1 then cvdtru=1;
   /* if the retained value is set, then assign
      it to the needed variable
   */
   if cvdtru=1 then cvd=1;
   drop cvdtru;
run;&lt;/PRE&gt;
&lt;P&gt;assume the data are sorted by ID and visit number as shown.&lt;/P&gt;</description>
    <pubDate>Fri, 03 Apr 2020 20:28:46 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-04-03T20:28:46Z</dc:date>
    <item>
      <title>Retain value based on the condition per ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-value-based-on-the-condition-per-ID/m-p/637345#M189453</link>
      <description>&lt;P&gt;Hi SAS programmers,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please help me with the following problem?&lt;/P&gt;&lt;P&gt;My data is:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; data have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; input id visit cvd;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; datalines;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; 1&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp;3&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; 1&amp;nbsp;&amp;nbsp; 5&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp;9 &amp;nbsp;&amp;nbsp;&amp;nbsp;0&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; 2&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; 2&amp;nbsp; 4&amp;nbsp;&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; 2&amp;nbsp; 5&amp;nbsp; &amp;nbsp;&amp;nbsp;0&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp;9 &amp;nbsp;&amp;nbsp;&amp;nbsp;.&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; ;&lt;/P&gt;&lt;P&gt;If a subject had cardiovascular even (CVD=1), I need them to retain CVD=1 in all subsequent visits. Data are unbalanced, measurements are taken for participants at different visits, and there are missing values. So I need to preserve missingness and zeros, unless subjects hits CVD=1, then “1” needs to be carried on until the last visit per id.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help/tips will be highly appreciated!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 20:14:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-value-based-on-the-condition-per-ID/m-p/637345#M189453</guid>
      <dc:creator>Dinurik</dc:creator>
      <dc:date>2020-04-03T20:14:51Z</dc:date>
    </item>
    <item>
      <title>Re: Retain value based on the condition per ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-value-based-on-the-condition-per-ID/m-p/637348#M189454</link>
      <description>&lt;P&gt;Here is one way for the given data.&lt;/P&gt;
&lt;PRE&gt; data have;
    input id visit cvd;
datalines;&lt;BR /&gt;1   1    0
1   3    0
1   5    1
1   9    0
2   3    .
2  4    1
2  5    0
2   9    .
;


data want;
   set have;
   by id;
   retain cvdtru;
   /* reset the retained value for the first visit
      of each id
   */
   if first.id then call missing(cvdtru);
   /* when find the cvd=1 then set the retained value*/
   if cvd=1 then cvdtru=1;
   /* if the retained value is set, then assign
      it to the needed variable
   */
   if cvdtru=1 then cvd=1;
   drop cvdtru;
run;&lt;/PRE&gt;
&lt;P&gt;assume the data are sorted by ID and visit number as shown.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 20:28:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-value-based-on-the-condition-per-ID/m-p/637348#M189454</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-03T20:28:46Z</dc:date>
    </item>
    <item>
      <title>Re: Retain value based on the condition per ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-value-based-on-the-condition-per-ID/m-p/637353#M189456</link>
      <description>&lt;P&gt;Thank you so much! It worked.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 20:44:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-value-based-on-the-condition-per-ID/m-p/637353#M189456</guid>
      <dc:creator>Dinurik</dc:creator>
      <dc:date>2020-04-03T20:44:14Z</dc:date>
    </item>
  </channel>
</rss>

