<?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 to add observation with previous values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/611217#M178100</link>
    <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; Thank you so much for your explanation....</description>
    <pubDate>Thu, 12 Dec 2019 06:40:27 GMT</pubDate>
    <dc:creator>sasuser123123</dc:creator>
    <dc:date>2019-12-12T06:40:27Z</dc:date>
    <item>
      <title>How to add observation with previous values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/607249#M176467</link>
      <description>Hello!&lt;BR /&gt;I've data where all lab results along with the 8 visits , So some subjects were not visits all 8 visits.So I need to retain the previous visit observation when visit is missing..like&lt;BR /&gt;&lt;BR /&gt;Data new;&lt;BR /&gt;Input Id $ visit val1 val2;&lt;BR /&gt;Datalines;&lt;BR /&gt;ABC123 1 50 54&lt;BR /&gt;ABC123 2 33 33&lt;BR /&gt;ABC123 3 21 44&lt;BR /&gt;ABC123 4 33 64&lt;BR /&gt;ABC121 1 90 34&lt;BR /&gt;ABC121 2 32 39&lt;BR /&gt;ABC121 3 51 24&lt;BR /&gt;ABC122 1 73 83&lt;BR /&gt;ABC122 2 10 14&lt;BR /&gt;ABC122 3 53 77&lt;BR /&gt;ABC124 1 50 54&lt;BR /&gt;ABC124 2 32 33&lt;BR /&gt;ABC124 3 51 94&lt;BR /&gt;ABC124 4 33 44&lt;BR /&gt;;&lt;BR /&gt;Run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;ID ABC121 and ABC122 have only three visits so I need to add 4th visit to those IDs with previous observation values. Could you please help me out how to do this one.&lt;BR /&gt;&lt;BR /&gt;Thank you!&lt;BR /&gt;Regards..&lt;BR /&gt;</description>
      <pubDate>Tue, 26 Nov 2019 09:56:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/607249#M176467</guid>
      <dc:creator>sasuser123123</dc:creator>
      <dc:date>2019-11-26T09:56:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to add observation with previous values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/607250#M176468</link>
      <description>&lt;P&gt;What does your desired output look like?&lt;/P&gt;</description>
      <pubDate>Tue, 26 Nov 2019 10:03:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/607250#M176468</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-11-26T10:03:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to add observation with previous values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/607255#M176471</link>
      <description>&lt;P&gt;Use by-group processing, and add a loop when last.id is reached:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ visit val1 val2;
datalines;
ABC123 1 50 54
ABC123 2 33 33
ABC123 3 21 44
ABC123 4 33 64
ABC121 1 90 34
ABC121 2 32 39
ABC121 3 51 24
ABC122 1 73 83
ABC122 2 10 14
ABC122 3 53 77
ABC124 1 50 54
ABC124 2 32 33
ABC124 3 51 94
ABC124 4 33 44
;

data want;
set have;
by id notsorted;
output;
if last.id
then do visit = visit + 1 to 4;
  output;
end;
run;

proc print data=want noobs;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;  id      visit    val1    val2

ABC123      1       50      54 
ABC123      2       33      33 
ABC123      3       21      44 
ABC123      4       33      64 
ABC121      1       90      34 
ABC121      2       32      39 
ABC121      3       51      24 
ABC121      4       51      24 
ABC122      1       73      83 
ABC122      2       10      14 
ABC122      3       53      77 
ABC122      4       53      77 
ABC124      1       50      54 
ABC124      2       32      33 
ABC124      3       51      94 
ABC124      4       33      44 
&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Nov 2019 10:11:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/607255#M176471</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-11-26T10:11:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to add observation with previous values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/607264#M176477</link>
      <description>Exactly like this..and it's perfectly working.&lt;BR /&gt;Thank you so much for your help..&lt;BR /&gt;And could you please explain why we used last.&lt;BR /&gt;</description>
      <pubDate>Tue, 26 Nov 2019 10:45:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/607264#M176477</guid>
      <dc:creator>sasuser123123</dc:creator>
      <dc:date>2019-11-26T10:45:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to add observation with previous values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/607275#M176481</link>
      <description>And also I forget to mention that the variable visit is in character format so what I did was firstly I converted it into numeric and then apply condition then again converted it into character. was this process is correct are is there any alternative......&lt;BR /&gt;Thank you!</description>
      <pubDate>Tue, 26 Nov 2019 11:25:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/607275#M176481</guid>
      <dc:creator>sasuser123123</dc:creator>
      <dc:date>2019-11-26T11:25:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to add observation with previous values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/607283#M176488</link>
      <description>&lt;P&gt;By-group processing is initiated with the by statement; for every variable in the by statement, automatic boolean variables first. and last. are created, which signal when a group starts and when it ends.&lt;/P&gt;
&lt;P&gt;My code runs a do loop when the last observation of a group is reached, and the do loop is specified in a way that it only runs when the last observation of a group has a visit number smaller than 4.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Nov 2019 11:49:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/607283#M176488</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-11-26T11:49:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to add observation with previous values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/607285#M176490</link>
      <description>&lt;P&gt;If you only have values 1 to 4, it makes sense to save space by storing it as $1 (numeric variables need at least 3 bytes).&lt;/P&gt;
&lt;P&gt;Still, if you do not have space issues, storing such values as numeric might be better.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Nov 2019 11:51:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/607285#M176490</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-11-26T11:51:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to add observation with previous values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/607286#M176491</link>
      <description>Thank you so much !</description>
      <pubDate>Tue, 26 Nov 2019 11:53:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/607286#M176491</guid>
      <dc:creator>sasuser123123</dc:creator>
      <dc:date>2019-11-26T11:53:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to add observation with previous values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/610948#M178017</link>
      <description>Hello sir!&lt;BR /&gt;I've a doubt...&lt;BR /&gt;So if the second observation or third observation is missing instead of fourth observation per id in our raw data how to do locf for that condition like..&lt;BR /&gt;&lt;BR /&gt;data have;&lt;BR /&gt;input id $ visit val1 val2;&lt;BR /&gt;datalines;&lt;BR /&gt;ABC123 1 50 54&lt;BR /&gt;ABC123 2 33 33&lt;BR /&gt;ABC123 4 33 64&lt;BR /&gt;ABC121 1 90 34&lt;BR /&gt;ABC121 3 51 24&lt;BR /&gt;ABC122 1 73 83&lt;BR /&gt;ABC122 2 10 14&lt;BR /&gt;ABC122 3 53 77&lt;BR /&gt;ABC124 1 50 54&lt;BR /&gt;ABC124 2 32 33&lt;BR /&gt;ABC124 3 51 94&lt;BR /&gt;ABC124 4 33 44&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 11 Dec 2019 10:48:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/610948#M178017</guid>
      <dc:creator>sasuser123123</dc:creator>
      <dc:date>2019-12-11T10:48:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to add observation with previous values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/610956#M178022</link>
      <description>&lt;P&gt;For this, you use the "look-ahead" technique:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ visit val1 val2;
datalines;
ABC123 1 50 54
ABC123 2 33 33
ABC123 4 33 64
ABC121 1 90 34
ABC121 3 51 24
ABC122 1 73 83
ABC122 2 10 14
ABC122 3 53 77
ABC124 1 50 54
ABC124 2 32 33
ABC124 3 51 94
ABC124 4 33 44
;

data want;
merge
  have
  have (
    firstobs=2
    keep=id visit
    rename=(id=_id visit=_visit)
  )
;
output;
if id ne _id then _visit = 5;
do visit = visit + 1 to _visit - 1;
  output;
end;
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 11 Dec 2019 11:39:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/610956#M178022</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-11T11:39:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to add observation with previous values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/611209#M178095</link>
      <description>It's very difficult to understand this program...&lt;BR /&gt;</description>
      <pubDate>Thu, 12 Dec 2019 05:39:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/611209#M178095</guid>
      <dc:creator>sasuser123123</dc:creator>
      <dc:date>2019-12-12T05:39:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to add observation with previous values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/611212#M178096</link>
      <description>Anyway thank you so much..</description>
      <pubDate>Thu, 12 Dec 2019 05:49:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/611212#M178096</guid>
      <dc:creator>sasuser123123</dc:creator>
      <dc:date>2019-12-12T05:49:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to add observation with previous values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/611214#M178097</link>
      <description>&lt;P&gt;I do a merge without a by; the firstobs=2 causes the second read in the merge to always be one observation ahead of the first read.&lt;/P&gt;
&lt;P&gt;I only keep two variables and rename them, so I have two additional variables in the PDV, "future" versions of the originals.&lt;/P&gt;
&lt;P&gt;One of these is used to detect if we're still in the same group (id), and the other gives me the next visit number.&lt;/P&gt;
&lt;P&gt;The do loop is structured in a way that it writes the current observation (one do loop iteration if next visit = current visit + 1), or multiple observations if there is a "hole". At the end of the group, I set a virtual 5th visit, so the loop works right up to visit 4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code was created mostly by applying Maxim 4: start with a simple idea (the look-ahead), run it, look where the result differs from the expectations, adapt and run again. Rinse, lather, repeat.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Dec 2019 06:27:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/611214#M178097</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-12T06:27:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to add observation with previous values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/611217#M178100</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; Thank you so much for your explanation....</description>
      <pubDate>Thu, 12 Dec 2019 06:40:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/611217#M178100</guid>
      <dc:creator>sasuser123123</dc:creator>
      <dc:date>2019-12-12T06:40:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to add observation with previous values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/611220#M178102</link>
      <description>And I'm unable to understand that why we used first obs option=2</description>
      <pubDate>Thu, 12 Dec 2019 07:23:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/611220#M178102</guid>
      <dc:creator>sasuser123123</dc:creator>
      <dc:date>2019-12-12T07:23:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to add observation with previous values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/611223#M178104</link>
      <description>&lt;P&gt;Read the first sentence of my previous post again. Also see the &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=ledsoptsref&amp;amp;docsetTarget=p0wjxoxrco6dsgn1ls5n3mbybcng.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;documentation&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Dec 2019 07:31:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-add-observation-with-previous-values/m-p/611223#M178104</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-12T07:31:29Z</dc:date>
    </item>
  </channel>
</rss>

