<?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: How do I conditionally replace the missing records in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-conditionally-replace-the-missing-records/m-p/504272#M134922</link>
    <description>&lt;P&gt;As posted under your original problem, here's a one -step method that assumes your data set is already in order:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;ID_counter=0;&lt;/P&gt;
&lt;P&gt;do until (last.id);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;by ID;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;ID_counter + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if first_nonmissing = . and treatment &amp;gt; ' ' then first_nonmissing = ID_counter;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if treatment &amp;gt; ' ' then last_nonmissing = ID_counter;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;ID_counter = 0;&lt;/P&gt;
&lt;P&gt;do until (last.ID);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;by ID;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;ID_counter + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if treatment = ' ' then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if ID_counter &amp;lt; first_nonmissing then treatment = 'Missing';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else if ID_counter &amp;lt; last_nonmissing then treatment = 'Untrt';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else treatment = 'Discontinue';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;drop first_nonmissing last_nonmissing ID_counter;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that if an ID has 100% missing values, they will become "Discontinue".&amp;nbsp; The outcome is slightly different, compared to the post on your original question.&lt;/P&gt;</description>
    <pubDate>Mon, 15 Oct 2018 12:52:07 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2018-10-15T12:52:07Z</dc:date>
    <item>
      <title>How do I conditionally replace the missing records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-conditionally-replace-the-missing-records/m-p/504235#M134913</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am a new SAS learner, I can not use answers' program flexibly. For each ID, I want to replace the missing records before the first non-missing Treatment as 'Missing'; to replace missing records after the last non-missing Treatment as 'Discontinue'; to replace the missing records between the first non-missing Treatment and the last non-missing Treatment as 'Untrt'. Could you please give me a hand? Thank you in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
input ID$ Month$ Treatment$ NEW_Treatment$ ;
cards;
1 Month_1 . Missing
1 Month_2 . Missing
1 Month_3 A A
1 Month_4 . Untrt
1 Month_5 B B
1 Month_6 . Untrt
1 Month_7 C C
1 Month_8 . Untrt
1 Month_9 . Untrt
1 Month_10 . Untrt
1 Month_11 E E
1 Month_12 . Discontinue
2 Month_1 B B
2 Month_2 C C
2 Month_3 . Untrt
2 Month_4 D D
2 Month_5 . Untrt
2 Month_6 . Untrt
2 Month_7 . Untrt
2 Month_8 . Untrt
2 Month_9 E E
2 Month_10 . Untrt
2 Month_11 . Untrt
2 Month_12 A A
3 Month_1 . Missing
3 Month_2 . Missing
3 Month_3 . Missing
3 Month_4 D D
3 Month_5 . Untrt
3 Month_6 . Untrt
3 Month_7 . Untrt
3 Month_8 . Untrt
3 Month_9 E E
3 Month_10 . Discontinue
3 Month_11 . Discontinue
3 Month_12 . Discontinue
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Oct 2018 09:05:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-conditionally-replace-the-missing-records/m-p/504235#M134913</guid>
      <dc:creator>Fangfang</dc:creator>
      <dc:date>2018-10-15T09:05:14Z</dc:date>
    </item>
    <item>
      <title>Re: How do I conditionally replace the missing records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-conditionally-replace-the-missing-records/m-p/504270#M134921</link>
      <description>&lt;P&gt;Sort in reverse chronological order to set the "Discontinue", then sort back into original order to set the other values:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
input ID$ Month Treatment$;
cards;
1 1 . Missing
1 2 . Missing
1 3 A A
1 4 . Untrt
1 5 B B
1 6 . Untrt
1 7 C C
1 8 . Untrt
1 9 . Untrt
1 10 . Untrt
1 11 E E
1 12 . Discontinue
2 1 B B
2 2 C C
2 3 . Untrt
2 4 D D
2 5 . Untrt
2 6 . Untrt
2 7 . Untrt
2 8 . Untrt
2 9 E E
2 10 . Untrt
2 11 . Untrt
2 12 A A
3 1 . Missing
3 2 . Missing
3 3 . Missing
3 4 D D
3 5 . Untrt
3 6 . Untrt
3 7 . Untrt
3 8 . Untrt
3 9 E E
3 10 . Discontinue
3 11 . Discontinue
3 12 . Discontinue
;
run;

proc sort data=have;
by id descending month;
run;

data int;
set have;
by id;
retain flag;
if first.id then flag = 1;
if not missing(treatment) then flag = 0;
else if flag then new_treatment = 'Discontinue';
run;

proc sort data=int;
by id month;
run;

data want;
set int;
by id;
retain flag;
if first.id then flag = 1;
if not missing(treatment)
then do;
  flag = 0;
  new_treatment = treatment;
end;
else if missing(new_treatment)
then do;
  if flag
  then new_treatment = 'Missing';
  else new_treatment = 'Untrt';
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that I made month numeric to facilitate correct sorting order. And that you will at least need a year column in real life application.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Oct 2018 12:43:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-conditionally-replace-the-missing-records/m-p/504270#M134921</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-10-15T12:43:56Z</dc:date>
    </item>
    <item>
      <title>Re: How do I conditionally replace the missing records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-conditionally-replace-the-missing-records/m-p/504272#M134922</link>
      <description>&lt;P&gt;As posted under your original problem, here's a one -step method that assumes your data set is already in order:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;ID_counter=0;&lt;/P&gt;
&lt;P&gt;do until (last.id);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;by ID;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;ID_counter + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if first_nonmissing = . and treatment &amp;gt; ' ' then first_nonmissing = ID_counter;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if treatment &amp;gt; ' ' then last_nonmissing = ID_counter;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;ID_counter = 0;&lt;/P&gt;
&lt;P&gt;do until (last.ID);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;by ID;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;ID_counter + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if treatment = ' ' then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if ID_counter &amp;lt; first_nonmissing then treatment = 'Missing';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else if ID_counter &amp;lt; last_nonmissing then treatment = 'Untrt';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else treatment = 'Discontinue';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;drop first_nonmissing last_nonmissing ID_counter;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that if an ID has 100% missing values, they will become "Discontinue".&amp;nbsp; The outcome is slightly different, compared to the post on your original question.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Oct 2018 12:52:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-conditionally-replace-the-missing-records/m-p/504272#M134922</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-10-15T12:52:07Z</dc:date>
    </item>
  </channel>
</rss>

