<?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: Changing value of previous observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-observation/m-p/245899#M45941</link>
    <description>&lt;P&gt;&lt;SPAN&gt;Hello Grvsinghal,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;You could try using call execute as below:&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data b; set a; run;

data _null_;
  set a;
  by id;

  l_id=lag(id);
  l_status=lag(status);

  if id eq l_id and status eq 'A' and l_status='Z' then do;
    call execute('data b; set b;
                    if id eq '||id||' and _N_ le '||_N_||' and status="Z" then status="A";
                  run;');
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 25 Jan 2016 17:01:31 GMT</pubDate>
    <dc:creator>abbess</dc:creator>
    <dc:date>2016-01-25T17:01:31Z</dc:date>
    <item>
      <title>Changing value of previous observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-observation/m-p/10822#M917</link>
      <description>This is the dataset I have&lt;BR /&gt;
ID Status&lt;BR /&gt;
1 A&lt;BR /&gt;
1 A&lt;BR /&gt;
1 Z&lt;BR /&gt;
1 Z&lt;BR /&gt;
1 A&lt;BR /&gt;
1 Z&lt;BR /&gt;
2 A&lt;BR /&gt;
2 A&lt;BR /&gt;
2 Z&lt;BR /&gt;
2 Z&lt;BR /&gt;
&lt;BR /&gt;
If status for any particular ID changes from Z to A, I want to update its value as A in all previous observations of that ID&lt;BR /&gt;
So this is what i want after processing&lt;BR /&gt;
&lt;BR /&gt;
1 A&lt;BR /&gt;
1 A&lt;BR /&gt;
&lt;B&gt;1 A&lt;BR /&gt;
1 A&lt;/B&gt;&lt;BR /&gt;
1 A&lt;BR /&gt;
1 Z&lt;BR /&gt;
2 A&lt;BR /&gt;
2 A&lt;BR /&gt;
2 Z&lt;BR /&gt;
2 Z&lt;BR /&gt;
&lt;BR /&gt;
I tried this &lt;BR /&gt;
data c;&lt;BR /&gt;
set b;&lt;BR /&gt;
by id;&lt;BR /&gt;
if status = 'A' and lag(status) = 'Z' then lag(status)='A';&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
But I get a dataset C with 0 observations. My original dataset B can have thousands of IDs and for some of them status may change from Z to A, so for all such IDs, I want to update status from Z to A as shown above</description>
      <pubDate>Fri, 10 Jun 2011 12:08:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-observation/m-p/10822#M917</guid>
      <dc:creator>grvsinghal</dc:creator>
      <dc:date>2011-06-10T12:08:03Z</dc:date>
    </item>
    <item>
      <title>Re: Changing value of previous observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-observation/m-p/10823#M918</link>
      <description>Hello Grvsinghal,&lt;BR /&gt;
&lt;BR /&gt;
This is a possible solution:&lt;BR /&gt;
[pre]&lt;BR /&gt;
data i;&lt;BR /&gt;
  input ID Status $;&lt;BR /&gt;
  datalines;&lt;BR /&gt;
1 A&lt;BR /&gt;
1 A&lt;BR /&gt;
1 Z&lt;BR /&gt;
1 Z&lt;BR /&gt;
1 A&lt;BR /&gt;
1 Z&lt;BR /&gt;
2 A&lt;BR /&gt;
2 A&lt;BR /&gt;
2 Z&lt;BR /&gt;
2 Z&lt;BR /&gt;
run;&lt;BR /&gt;
proc transpose data=i out=t prefix=s;&lt;BR /&gt;
  var status;&lt;BR /&gt;
  by id;&lt;BR /&gt;
run;&lt;BR /&gt;
data r0;&lt;BR /&gt;
  set t;&lt;BR /&gt;
  array ss {*} s:;&lt;BR /&gt;
  do i=1 to DIM(ss)-1;&lt;BR /&gt;
     if ss[i]="Z" and ss[i+1]="A" then &lt;BR /&gt;
     do j=1 to i;&lt;BR /&gt;
       ss[j]="A";&lt;BR /&gt;
     end;&lt;BR /&gt;
  end;&lt;BR /&gt;
  by id;&lt;BR /&gt;
run;&lt;BR /&gt;
proc transpose data=r0 out=r(drop=_:);&lt;BR /&gt;
  var s:;&lt;BR /&gt;
  by ID;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
Sincerely,&lt;BR /&gt;
SPR</description>
      <pubDate>Fri, 10 Jun 2011 19:52:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-observation/m-p/10823#M918</guid>
      <dc:creator>SPR</dc:creator>
      <dc:date>2011-06-10T19:52:21Z</dc:date>
    </item>
    <item>
      <title>Re: Changing value of previous observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-observation/m-p/10824#M919</link>
      <description>Hi:&lt;BR /&gt;
  This Tech Support note outlines some of the problems you might encounter when you call a LAG function conditionally. (inside an IF condition) &lt;A href="http://support.sas.com/kb/24/694.html" target="_blank"&gt;http://support.sas.com/kb/24/694.html&lt;/A&gt;  The note says:&lt;BR /&gt;
"A LAGn function that is executed conditionally will store and return values only from the observations for which the condition is satisfied. " This means that the LAG queue/stack might not contain the values you expect if LAG is not called for every observation.&lt;BR /&gt;
&lt;BR /&gt;
And this note shows the WRONG and RIGHT ways to use the LAG function:&lt;BR /&gt;
&lt;A href="http://support.sas.com/kb/24/665.html" target="_blank"&gt;http://support.sas.com/kb/24/665.html&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
cynthia</description>
      <pubDate>Fri, 10 Jun 2011 19:59:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-observation/m-p/10824#M919</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2011-06-10T19:59:04Z</dc:date>
    </item>
    <item>
      <title>Re: Changing value of previous observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-observation/m-p/10825#M920</link>
      <description>[pre]&lt;BR /&gt;
data i;&lt;BR /&gt;
  input ID Status $;&lt;BR /&gt;
  count+1;&lt;BR /&gt;
  datalines;&lt;BR /&gt;
1 A&lt;BR /&gt;
1 A&lt;BR /&gt;
1 Z&lt;BR /&gt;
1 Z&lt;BR /&gt;
1 A&lt;BR /&gt;
1 Z&lt;BR /&gt;
2 A&lt;BR /&gt;
2 A&lt;BR /&gt;
2 Z&lt;BR /&gt;
2 Z&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
data temp(rename=(count=_count status=_status));&lt;BR /&gt;
 set i;&lt;BR /&gt;
 if id eq lag(id) and status='A' and lag(status)='Z';&lt;BR /&gt;
run; &lt;BR /&gt;
data want(drop=_: count);&lt;BR /&gt;
 merge i temp;&lt;BR /&gt;
 by id;&lt;BR /&gt;
 if count lt _count then status=_status;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Mon, 13 Jun 2011 08:09:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-observation/m-p/10825#M920</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-06-13T08:09:24Z</dc:date>
    </item>
    <item>
      <title>Re: Changing value of previous observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-observation/m-p/10826#M921</link>
      <description>Thank you SPR, cynthia and Ksharp. Now I know how to use lag function, and my problem is solved. Thanks a lot</description>
      <pubDate>Mon, 13 Jun 2011 14:25:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-observation/m-p/10826#M921</guid>
      <dc:creator>grvsinghal</dc:creator>
      <dc:date>2011-06-13T14:25:06Z</dc:date>
    </item>
    <item>
      <title>Re: Changing value of previous observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-observation/m-p/245899#M45941</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hello Grvsinghal,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;You could try using call execute as below:&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data b; set a; run;

data _null_;
  set a;
  by id;

  l_id=lag(id);
  l_status=lag(status);

  if id eq l_id and status eq 'A' and l_status='Z' then do;
    call execute('data b; set b;
                    if id eq '||id||' and _N_ le '||_N_||' and status="Z" then status="A";
                  run;');
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Jan 2016 17:01:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-value-of-previous-observation/m-p/245899#M45941</guid>
      <dc:creator>abbess</dc:creator>
      <dc:date>2016-01-25T17:01:31Z</dc:date>
    </item>
  </channel>
</rss>

