<?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 retain a value from pre ious observation in Statistical Procedures</title>
    <link>https://communities.sas.com/t5/Statistical-Procedures/How-retain-a-value-from-pre-ious-observation/m-p/864594#M42750</link>
    <description>Hi all,&lt;BR /&gt;Please help me with below query&lt;BR /&gt;If I have a variable with a test value and corresponding result. I need to copy the same value to other blank observations as below:&lt;BR /&gt;Having:&lt;BR /&gt;Test  Result&lt;BR /&gt;A        12.6&lt;BR /&gt;A&lt;BR /&gt;A&lt;BR /&gt;A&lt;BR /&gt;A&lt;BR /&gt;B        14.9&lt;BR /&gt;B&lt;BR /&gt;B&lt;BR /&gt;B&lt;BR /&gt;B&lt;BR /&gt;B&lt;BR /&gt;&lt;BR /&gt;Need as below:&lt;BR /&gt;Test  Result&lt;BR /&gt;A        12.6&lt;BR /&gt;A        12.6&lt;BR /&gt;A        12.6&lt;BR /&gt;A        12.6&lt;BR /&gt;A         12.6&lt;BR /&gt;B        14.9&lt;BR /&gt;B       14.9&lt;BR /&gt;B      14.9&lt;BR /&gt;B         14.9&lt;BR /&gt;B      14.9  &lt;BR /&gt;B      14.9</description>
    <pubDate>Thu, 16 Mar 2023 16:13:33 GMT</pubDate>
    <dc:creator>r3570</dc:creator>
    <dc:date>2023-03-16T16:13:33Z</dc:date>
    <item>
      <title>How retain a value from pre ious observation</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-retain-a-value-from-pre-ious-observation/m-p/864594#M42750</link>
      <description>Hi all,&lt;BR /&gt;Please help me with below query&lt;BR /&gt;If I have a variable with a test value and corresponding result. I need to copy the same value to other blank observations as below:&lt;BR /&gt;Having:&lt;BR /&gt;Test  Result&lt;BR /&gt;A        12.6&lt;BR /&gt;A&lt;BR /&gt;A&lt;BR /&gt;A&lt;BR /&gt;A&lt;BR /&gt;B        14.9&lt;BR /&gt;B&lt;BR /&gt;B&lt;BR /&gt;B&lt;BR /&gt;B&lt;BR /&gt;B&lt;BR /&gt;&lt;BR /&gt;Need as below:&lt;BR /&gt;Test  Result&lt;BR /&gt;A        12.6&lt;BR /&gt;A        12.6&lt;BR /&gt;A        12.6&lt;BR /&gt;A        12.6&lt;BR /&gt;A         12.6&lt;BR /&gt;B        14.9&lt;BR /&gt;B       14.9&lt;BR /&gt;B      14.9&lt;BR /&gt;B         14.9&lt;BR /&gt;B      14.9  &lt;BR /&gt;B      14.9</description>
      <pubDate>Thu, 16 Mar 2023 16:13:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-retain-a-value-from-pre-ious-observation/m-p/864594#M42750</guid>
      <dc:creator>r3570</dc:creator>
      <dc:date>2023-03-16T16:13:33Z</dc:date>
    </item>
    <item>
      <title>Re: How retain a value from pre ious observation</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-retain-a-value-from-pre-ious-observation/m-p/864597#M42751</link>
      <description>&lt;P&gt;Use a RETAINed variable which you discard later.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by test;
retain _result;
if first.test then _result = .;
if result ne .
then _result = result;
else result = _result;
drop _result;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Mar 2023 16:18:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-retain-a-value-from-pre-ious-observation/m-p/864597#M42751</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-03-16T16:18:11Z</dc:date>
    </item>
    <item>
      <title>Re: How retain a value from pre ious observation</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-retain-a-value-from-pre-ious-observation/m-p/864599#M42752</link>
      <description>&lt;P&gt;Amazingly enough the function is RETAIN, but you want to use it with a new variable.&lt;/P&gt;
&lt;PRE&gt;data have;
  input Test $ Result;
datalines;
A 12.6
A .
A .
A .
A .
B 14.9
B .
B .
B .
B .
B .
;


data want;
   set have;
   by test notsorted;
   retain tempresult;
   if first.test then tempresult=.;
   if not missing(result) then tempresult=result;
   if missing (result) then result=tempresult;
   drop tempresult;
run;&lt;/PRE&gt;
&lt;P&gt;The data step creates a set similar to your description.&lt;/P&gt;
&lt;P&gt;The BY statement in the Want data step assumes that your data is grouped by Test value if not actually sorted.&lt;/P&gt;
&lt;P&gt;Retain creates a variable that will hold values. If the value is to be character you should define the length before the Retain statement.&lt;/P&gt;
&lt;P&gt;The BY statement creates automatic variable that you can test to see if an observation is the first or last of a group. In this case it is used to reset the retained variable when the Test value changes to a new group.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Mar 2023 16:21:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-retain-a-value-from-pre-ious-observation/m-p/864599#M42752</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-03-16T16:21:55Z</dc:date>
    </item>
    <item>
      <title>Re: How retain a value from pre ious observation</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-retain-a-value-from-pre-ious-observation/m-p/864799#M42765</link>
      <description>&lt;PRE&gt;data have;
  input Test $ Result;
datalines;
A 12.6
A .
A .
A .
A .
B 14.9
B .
B .
B .
B .
B .
;

data want;
update have(obs=0) have;
by Test;
output;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Mar 2023 11:45:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-retain-a-value-from-pre-ious-observation/m-p/864799#M42765</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-03-17T11:45:06Z</dc:date>
    </item>
  </channel>
</rss>

