<?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 How to retain a value until conditions met in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-until-conditions-met/m-p/857487#M338822</link>
    <description>&lt;P&gt;I have this table;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data WORK.input;
length ID $1. Type $10.;
infile datalines delimiter='#';
input ID $ Type $ Val sortorder;
datalines;
1#A#100#1
1#A#100#2
1#A#100#3
1#A#100#4
1#A#100#5
1#A#100#6
1#X#999999#7
1#X#999999#8
1#X#999999#9
1#X#999999#10
1#A#700#11
1#A#700#12
1#A#700#13
1#A#700#14
2#A#300#1
2#A#300#2
2#A#300#3
2#A#300#4
2#X#999999#5
2#X#999999#6
2#A#500#7
2#A#500#8
2#X#999999#9
2#A#800#10
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I've included a sort order column. What I'm trying to do is when we hit the first row where Type = X I want to retain the last known Val for each row thereafter until we hit type &amp;lt;&amp;gt; X then we will set the new Val.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried this code, but it appears to be working (mostly) except for lines 1, 11,21, and 24 are showing NULL or the Val on the X row.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.input2(DROP=sortorder);
set work.input;
LagVal = LAG(Val);
if first.ID then LagVal = .;
retain LastKnownValBeforeX;
if Type ne 'X' then LastKnownValBeforeX = LagVal;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;the picture below explains my issue and desired output:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80160i5BA4F951A5C2F981/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Thanks in advance;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 07 Feb 2023 05:19:43 GMT</pubDate>
    <dc:creator>SASAlex101</dc:creator>
    <dc:date>2023-02-07T05:19:43Z</dc:date>
    <item>
      <title>How to retain a value until conditions met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-until-conditions-met/m-p/857487#M338822</link>
      <description>&lt;P&gt;I have this table;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data WORK.input;
length ID $1. Type $10.;
infile datalines delimiter='#';
input ID $ Type $ Val sortorder;
datalines;
1#A#100#1
1#A#100#2
1#A#100#3
1#A#100#4
1#A#100#5
1#A#100#6
1#X#999999#7
1#X#999999#8
1#X#999999#9
1#X#999999#10
1#A#700#11
1#A#700#12
1#A#700#13
1#A#700#14
2#A#300#1
2#A#300#2
2#A#300#3
2#A#300#4
2#X#999999#5
2#X#999999#6
2#A#500#7
2#A#500#8
2#X#999999#9
2#A#800#10
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I've included a sort order column. What I'm trying to do is when we hit the first row where Type = X I want to retain the last known Val for each row thereafter until we hit type &amp;lt;&amp;gt; X then we will set the new Val.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried this code, but it appears to be working (mostly) except for lines 1, 11,21, and 24 are showing NULL or the Val on the X row.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.input2(DROP=sortorder);
set work.input;
LagVal = LAG(Val);
if first.ID then LagVal = .;
retain LastKnownValBeforeX;
if Type ne 'X' then LastKnownValBeforeX = LagVal;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;the picture below explains my issue and desired output:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80160i5BA4F951A5C2F981/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Thanks in advance;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2023 05:19:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-until-conditions-met/m-p/857487#M338822</guid>
      <dc:creator>SASAlex101</dc:creator>
      <dc:date>2023-02-07T05:19:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain a value until conditions met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-until-conditions-met/m-p/857496#M338827</link>
      <description>&lt;P&gt;You need a RETAINed variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set input;
by id;
retain newval;
if first.id then newval = .;
if type = "X" then newval = val;
else if newval ne . then val = newval;
drop newval;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Feb 2023 06:41:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-until-conditions-met/m-p/857496#M338827</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-02-07T06:41:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain a value until conditions met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-until-conditions-met/m-p/857557#M338837</link>
      <description>&lt;P&gt;This didn't give the correct output. 99999 is repeated through the A rows...&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2023 14:46:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-until-conditions-met/m-p/857557#M338837</guid>
      <dc:creator>SASAlex101</dc:creator>
      <dc:date>2023-02-07T14:46:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain a value until conditions met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-until-conditions-met/m-p/857559#M338838</link>
      <description>&lt;P&gt;GOT IT!&lt;BR /&gt;&lt;BR /&gt;data output;&lt;BR /&gt;set input;&lt;BR /&gt;by id sortorder Type;&lt;BR /&gt;retain newvar;&lt;BR /&gt;if Type='A' then newvar= val;&lt;BR /&gt;if Type ne 'X' then newvar = newvar;&lt;BR /&gt;output;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2023 14:53:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-until-conditions-met/m-p/857559#M338838</guid>
      <dc:creator>SASAlex101</dc:creator>
      <dc:date>2023-02-07T14:53:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain a value until conditions met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-until-conditions-met/m-p/857560#M338839</link>
      <description>Props to Kurt for helping find this solution!</description>
      <pubDate>Tue, 07 Feb 2023 14:53:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-until-conditions-met/m-p/857560#M338839</guid>
      <dc:creator>SASAlex101</dc:creator>
      <dc:date>2023-02-07T14:53:35Z</dc:date>
    </item>
  </channel>
</rss>

