<?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 Replace values in an observation in data manipulation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Replace-values-in-an-observation-in-data-manipulation/m-p/836752#M330838</link>
    <description>&lt;P&gt;Hi, I have a variable with a series 1 and 0 in dataset, most of the time they are like "111011011" for 9 observations for example. Most importantly I want to change the first 0 to 1 and keep the second 0. There are more specific rules and I will display it via an example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input ID time var;
    cards;
1 1 1
1 2 1
1 3 0
1 4 1
1 5 1
1 6 1
1 7 0
1 8 1
2 1 1
2 2 1
2 3 0
2 4 0
2 5 1
2 6 1
2 7 0
2 8 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In this example, I have two IDs with different pattern of the variable "var" over time. I wanted to replace the first 0 by 1 &lt;STRONG&gt;only when this 0 is followed by 1 &lt;/STRONG&gt;(e.g., var = 0 for ID 1 at time 3). I do not replace 0 for ID 2 at time 3 because it is also 0 for time 4. The 0 at time 7 for ID 2 will also not replaced by 1 although the pattern is like "101", because it is not the first 0 for this ID.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The final dataset I want is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    input ID time var;
    cards;
1 1 1
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 0
1 8 1
2 1 1
2 2 1
2 3 0
2 4 0
2 5 1
2 6 1
2 7 0
2 8 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is there any way to accomplish that? Thanks!&lt;/P&gt;</description>
    <pubDate>Tue, 04 Oct 2022 17:21:00 GMT</pubDate>
    <dc:creator>Chaupak</dc:creator>
    <dc:date>2022-10-04T17:21:00Z</dc:date>
    <item>
      <title>Replace values in an observation in data manipulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-values-in-an-observation-in-data-manipulation/m-p/836752#M330838</link>
      <description>&lt;P&gt;Hi, I have a variable with a series 1 and 0 in dataset, most of the time they are like "111011011" for 9 observations for example. Most importantly I want to change the first 0 to 1 and keep the second 0. There are more specific rules and I will display it via an example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input ID time var;
    cards;
1 1 1
1 2 1
1 3 0
1 4 1
1 5 1
1 6 1
1 7 0
1 8 1
2 1 1
2 2 1
2 3 0
2 4 0
2 5 1
2 6 1
2 7 0
2 8 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In this example, I have two IDs with different pattern of the variable "var" over time. I wanted to replace the first 0 by 1 &lt;STRONG&gt;only when this 0 is followed by 1 &lt;/STRONG&gt;(e.g., var = 0 for ID 1 at time 3). I do not replace 0 for ID 2 at time 3 because it is also 0 for time 4. The 0 at time 7 for ID 2 will also not replaced by 1 although the pattern is like "101", because it is not the first 0 for this ID.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The final dataset I want is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    input ID time var;
    cards;
1 1 1
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 0
1 8 1
2 1 1
2 2 1
2 3 0
2 4 0
2 5 1
2 6 1
2 7 0
2 8 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is there any way to accomplish that? Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 04 Oct 2022 17:21:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-values-in-an-observation-in-data-manipulation/m-p/836752#M330838</guid>
      <dc:creator>Chaupak</dc:creator>
      <dc:date>2022-10-04T17:21:00Z</dc:date>
    </item>
    <item>
      <title>Re: Replace values in an observation in data manipulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-values-in-an-observation-in-data-manipulation/m-p/836757#M330842</link>
      <description>&lt;P&gt;Looking ahead is difficult.&amp;nbsp; Here is a method using a second SET statement that re-reads the data starting from the second observation.&amp;nbsp; Tack on an extra empty observation to have the number of observations match.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will also need to keep track of whether or not you have found a zero to be able to detect the first one.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  by id;
  set have(keep=var rename=(var=next) firstobs=2) have(obs=1 drop=_all_);
  retain found;
  if last.id then call missing(next);
  if first.id then found=0;
  if var=0 and not found then do;
    found=1;
    if next=1 then var=1;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    ID    time    var    next    found

  1     1      1      1       1       0
  2     1      2      1       0       0
  3     1      3      1       1       1
  4     1      4      1       1       1
  5     1      5      1       1       1
  6     1      6      1       0       1
  7     1      7      0       1       1
  8     1      8      1       .       1
  9     2      1      1       1       0
 10     2      2      1       0       0
 11     2      3      0       0       1
 12     2      4      0       1       1
 13     2      5      1       1       1
 14     2      6      1       0       1
 15     2      7      0       1       1
 16     2      8      1       .       1
&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Oct 2022 17:48:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-values-in-an-observation-in-data-manipulation/m-p/836757#M330842</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-10-04T17:48:30Z</dc:date>
    </item>
    <item>
      <title>Re: Replace values in an observation in data manipulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-values-in-an-observation-in-data-manipulation/m-p/836775#M330849</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/397340"&gt;@Chaupak&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If dataset HAVE is sorted by ID TIME and variable VAR contains only 0s and 1s, you can also use the implied look-ahead enabled by a BY statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=f);
set have;
by id var notsorted;
if first.id then f=.;
if ~f &amp;amp; var=0 &amp;amp; ~last.id then do;
  var=last.var;
  f+1;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Oct 2022 18:52:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-values-in-an-observation-in-data-manipulation/m-p/836775#M330849</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-10-04T18:52:31Z</dc:date>
    </item>
    <item>
      <title>Re: Replace values in an observation in data manipulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-values-in-an-observation-in-data-manipulation/m-p/836912#M330916</link>
      <description>&lt;PRE&gt;data have;
    input ID time var;
    cards;
1 1 1
1 2 1
1 3 0
1 4 1
1 5 1
1 6 1
1 7 0
1 8 1
2 1 1
2 2 1
2 3 0
2 4 0
2 5 1
2 6 1
2 7 0
2 8 1
;
run;

data want;
 merge have have(keep=id var rename=(id=_id var=_var) firstobs=2);
 retain found .;
 if id ne lag(id) then found=.;
 if not found and var=0 and id=_id and _var=1 and (id ne lag(id) or (id=lag(id) and lag(var)=1)) then do;found=1;var=1;end;
 if var=0 then found=1;
drop found _:;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Oct 2022 10:59:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-values-in-an-observation-in-data-manipulation/m-p/836912#M330916</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-10-05T10:59:08Z</dc:date>
    </item>
  </channel>
</rss>

