<?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 Entering Second Observation into Previous Observation Column in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Entering-Second-Observation-into-Previous-Observation-Column/m-p/258910#M49977</link>
    <description>&lt;P&gt;I have the following data set:&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/2457i556A0DE603B88B92/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="dataset.JPG" title="dataset.JPG" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for each paired record (where paired = first and second), i'm trying to get the field in car2&amp;nbsp;to output in the observation above it so when car1 has a value, car2 has the value from below. &amp;nbsp;it should look like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/2459i477BEA30017179E7/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="dataset2.JPG" title="dataset2.JPG" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i am having the hardest time thinking of how to output this way. &amp;nbsp;would it be something with _n_+1? &amp;nbsp;nobs? &amp;nbsp;my code is below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data work.want;&lt;BR /&gt;set work.filtered_hu nobs=nobs;&lt;BR /&gt;by date_stopped time train_id car_pos;&lt;BR /&gt;firstrecord=first.TIME;&lt;BR /&gt;lastrecord=last.TIME;&lt;/P&gt;
&lt;P&gt;***define pairings***;&lt;BR /&gt;if firstrecord=1 and lastrecord=1 then paired = "single";&lt;BR /&gt;if firstrecord=1 and lastrecord=0 then paired = "first car";&lt;BR /&gt;if firstrecord=0 and lastrecord=1 then paired = "second car";&lt;/P&gt;
&lt;P&gt;***define car1***;&lt;BR /&gt;if paired = "first" then car1=catx(" ",carint,number);&lt;BR /&gt;if paired = "single" then car1=catx(" ",carint,number);&lt;/P&gt;
&lt;P&gt;***define car2***;&lt;BR /&gt;if paired = "second" then car2=catx(" ",series,car_number);&lt;BR /&gt;if paired = "single" then car2=catx(" ",series,car_number);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 24 Mar 2016 18:39:56 GMT</pubDate>
    <dc:creator>laneylaners</dc:creator>
    <dc:date>2016-03-24T18:39:56Z</dc:date>
    <item>
      <title>Entering Second Observation into Previous Observation Column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Entering-Second-Observation-into-Previous-Observation-Column/m-p/258910#M49977</link>
      <description>&lt;P&gt;I have the following data set:&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/2457i556A0DE603B88B92/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="dataset.JPG" title="dataset.JPG" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for each paired record (where paired = first and second), i'm trying to get the field in car2&amp;nbsp;to output in the observation above it so when car1 has a value, car2 has the value from below. &amp;nbsp;it should look like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/2459i477BEA30017179E7/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="dataset2.JPG" title="dataset2.JPG" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i am having the hardest time thinking of how to output this way. &amp;nbsp;would it be something with _n_+1? &amp;nbsp;nobs? &amp;nbsp;my code is below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data work.want;&lt;BR /&gt;set work.filtered_hu nobs=nobs;&lt;BR /&gt;by date_stopped time train_id car_pos;&lt;BR /&gt;firstrecord=first.TIME;&lt;BR /&gt;lastrecord=last.TIME;&lt;/P&gt;
&lt;P&gt;***define pairings***;&lt;BR /&gt;if firstrecord=1 and lastrecord=1 then paired = "single";&lt;BR /&gt;if firstrecord=1 and lastrecord=0 then paired = "first car";&lt;BR /&gt;if firstrecord=0 and lastrecord=1 then paired = "second car";&lt;/P&gt;
&lt;P&gt;***define car1***;&lt;BR /&gt;if paired = "first" then car1=catx(" ",carint,number);&lt;BR /&gt;if paired = "single" then car1=catx(" ",carint,number);&lt;/P&gt;
&lt;P&gt;***define car2***;&lt;BR /&gt;if paired = "second" then car2=catx(" ",series,car_number);&lt;BR /&gt;if paired = "single" then car2=catx(" ",series,car_number);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Mar 2016 18:39:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Entering-Second-Observation-into-Previous-Observation-Column/m-p/258910#M49977</guid>
      <dc:creator>laneylaners</dc:creator>
      <dc:date>2016-03-24T18:39:56Z</dc:date>
    </item>
    <item>
      <title>Re: Entering Second Observation into Previous Observation Column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Entering-Second-Observation-into-Previous-Observation-Column/m-p/258917#M49982</link>
      <description>&lt;P&gt;Looking back is called lag, looking forward is called lead.&lt;/P&gt;
&lt;P&gt;There's some good posts on here on how to access the next record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're probably not going to get code if you post pictures as your data, SAS doesn't read pictures and I'm not typing in your data.&lt;/P&gt;
&lt;P&gt;You can create sample data for the forum by following the instructions here:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;http://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_blank"&gt;http://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And here's a bunch of resources that show how to access lead.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A part of me also wonders if you can't go back a step and clean it up easier.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT: Realized your transposing the data from the variable CAR_MARK - use transpose and merge back &amp;nbsp;is also another method.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Mar 2016 18:48:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Entering-Second-Observation-into-Previous-Observation-Column/m-p/258917#M49982</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-03-24T18:48:14Z</dc:date>
    </item>
    <item>
      <title>Re: Entering Second Observation into Previous Observation Column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Entering-Second-Observation-into-Previous-Observation-Column/m-p/258925#M49987</link>
      <description>&lt;P&gt;Thanks! &amp;nbsp;However, i'm not seeing the links you indicated for lead/accessing the next record.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Mar 2016 19:01:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Entering-Second-Observation-into-Previous-Observation-Column/m-p/258925#M49987</guid>
      <dc:creator>laneylaners</dc:creator>
      <dc:date>2016-03-24T19:01:52Z</dc:date>
    </item>
    <item>
      <title>Re: Entering Second Observation into Previous Observation Column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Entering-Second-Observation-into-Previous-Observation-Column/m-p/258931#M49989</link>
      <description>&lt;P&gt;No...apparently the wrong link posted...search does work though &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://communities.sas.com/t5/SAS-Communities-Library/How-to-simulate-the-LEAD-function-opposite-of-LAG/ta-p/232151" target="_blank"&gt;http://communities.sas.com/t5/SAS-Communities-Library/How-to-simulate-the-LEAD-function-opposite-of-LAG/ta-p/232151&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/2463i03BB9C488F3ED3C1/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="delete search sample.JPG" title="delete search sample.JPG" /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Mar 2016 19:04:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Entering-Second-Observation-into-Previous-Observation-Column/m-p/258931#M49989</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-03-24T19:04:56Z</dc:date>
    </item>
    <item>
      <title>Re: Entering Second Observation into Previous Observation Column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Entering-Second-Observation-into-Previous-Observation-Column/m-p/259002#M50026</link>
      <description>&lt;PRE&gt;
Are you sure the paired obs will always appear ?
And don't post your data as picture, post it as text ,better be SAS code. Nobody will like to type it for you ,if you want solution.


data have;
input paired $ car1 $ car2 $;
cards;
first X .
second . Y
single Z .
;
run;
data want;
 merge have 
   have(keep=paired  car1  car2 rename=(paired=_paired  car1=_car1 car2=_car2) firstobs=2);
 if paired='first' and _paired='second' then do;
  car1=coalescec(car1,_car1);
  car2=coalescec(car2,_car2);
 end;
 if lag(paired)='first' and paired='second' then call missing(car1,car2);
 drop _:;
 run;

&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Mar 2016 01:16:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Entering-Second-Observation-into-Previous-Observation-Column/m-p/259002#M50026</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-03-25T01:16:58Z</dc:date>
    </item>
  </channel>
</rss>

