<?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: Replacing Invalid Records Using Data from Another Record in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Invalid-Records-Using-Data-from-Another-Record/m-p/952678#M372304</link>
    <description>&lt;P&gt;Example input data in the form of a working data step for the variables needed. That way we can test code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then provide for the example what the expected/desired result is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Did your log show any interesting messages?&lt;/P&gt;
&lt;P&gt;Are all of those code variables actually numeric?&lt;/P&gt;
&lt;P&gt;You have a comment about "15 minutes". Are the variables involved SAS TIME or DATEtime variables? Comparisons as written would be looking at 15 second differences. If the values are not SAS time or datetime values then there can be all sorts of issues with how the actual values are stored and what is needed to determine "15 minutes". It may help to provide the output from Proc Contents for your data set Baseline_2023 to clarify any questions about your variables.&lt;/P&gt;</description>
    <pubDate>Thu, 05 Dec 2024 22:26:37 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2024-12-05T22:26:37Z</dc:date>
    <item>
      <title>Replacing Invalid Records Using Data from Another Record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Invalid-Records-Using-Data-from-Another-Record/m-p/952676#M372302</link>
      <description>&lt;P&gt;I am currently working on a dataset which contains records of daily time use for a large number of participants (American Time Use Survey activity data). I am trying to modify some data before proceeding with the next steps, but my SAS code is not working as intended.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here’s the condition I am trying to implement in the code:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;If a participant (same &lt;CODE&gt;TUCASEID&lt;/CODE&gt;) has only two commute periods (&lt;CODE&gt;TRCODE&lt;/CODE&gt; in 180501, 180503, 180504), where:&lt;UL&gt;&lt;LI&gt;One period occurs right before work (&lt;CODE&gt;TUTIER2CODE=05&lt;/CODE&gt;) and the other right after work.&lt;/LI&gt;&lt;LI&gt;he two periods differ by less than 15 minutes in duration (&lt;CODE&gt;TUACTDUR24&lt;/CODE&gt;).&lt;/LI&gt;&lt;LI&gt;One record of commute modality is valid (&lt;CODE&gt;TEWHERE&lt;/CODE&gt; between 12 and 21), but the other is invalid (&lt;CODE&gt;TEWHERE&lt;/CODE&gt; less than 12).&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;In this case, I want to replace the invalid modality with the valid modality.&lt;/P&gt;&lt;P&gt;I tried the code below, but the results are different from what I expected. Which parts need to be changed?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;data replaced_2023; set baseline_2023;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;by TUCASEID;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if TRCODE in (180501, 180503, 180504) then do;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if 12 &amp;lt;= TEWHERE &amp;lt;= 21 then valid_modality = 1;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else if TEWHERE &amp;lt; 12 then valid_modality = 0;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; end;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;retain prev_TUTIER1CODE prev_TUACTDUR24 prev_TEWHERE prev_valid_modality;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if first.TUCASEID then do;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; prev_TUTIER1CODE = .;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; prev_TUACTDUR24 = .;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; prev_TEWHERE = .;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; prev_valid_modality = .;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; end;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if TUTIER1CODE = 05 and prev_TUTIER1CODE = 05 and&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;abs(TUACTDUR24 - prev_TUACTDUR24) &amp;lt; 15 and&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;prev_valid_modality = 1 and valid_modality = 0 then TEWHERE = lag(prev_TEWHERE);&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;data last_replaced_2023; set replaced_2023;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;by TUCASEID;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if TRCODE in (180501, 180503, 180504) then do;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if 12 &amp;lt;= TEWHERE &amp;lt;= 21 then valid_modality = 1;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else if TEWHERE &amp;lt; 12 then valid_modality = 0;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; end;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;retain next_TUTIER2 next_TUACTDUR24 next_TEWHERE next_valid_modality;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if first.TUCASEID then do;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; next_TUTIER1CODE = .;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; next_TUACTDUR24 = .;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; next_TEWHERE = .;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; next_valid_modality = .;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; end;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if TUTIER1CODE = 05 and prev_TUTIER1 = 05 and&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;abs(TUACTDUR24 - prev_TUACTDUR24) &amp;lt; 15 and&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;next_valid_modality = 1 and next_modality = 0 then TEWHERE = lag(next_TEWHERE);&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Dec 2024 22:00:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Invalid-Records-Using-Data-from-Another-Record/m-p/952676#M372302</guid>
      <dc:creator>Sumin1</dc:creator>
      <dc:date>2024-12-05T22:00:48Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Invalid Records Using Data from Another Record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Invalid-Records-Using-Data-from-Another-Record/m-p/952678#M372304</link>
      <description>&lt;P&gt;Example input data in the form of a working data step for the variables needed. That way we can test code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then provide for the example what the expected/desired result is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Did your log show any interesting messages?&lt;/P&gt;
&lt;P&gt;Are all of those code variables actually numeric?&lt;/P&gt;
&lt;P&gt;You have a comment about "15 minutes". Are the variables involved SAS TIME or DATEtime variables? Comparisons as written would be looking at 15 second differences. If the values are not SAS time or datetime values then there can be all sorts of issues with how the actual values are stored and what is needed to determine "15 minutes". It may help to provide the output from Proc Contents for your data set Baseline_2023 to clarify any questions about your variables.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Dec 2024 22:26:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Invalid-Records-Using-Data-from-Another-Record/m-p/952678#M372304</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-12-05T22:26:37Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Invalid Records Using Data from Another Record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Invalid-Records-Using-Data-from-Another-Record/m-p/952688#M372309</link>
      <description>&lt;P&gt;Definitely recommend posting a working input dataset as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;suggested.&amp;nbsp; The one thing that jumps out is that you're using LAG() conditionally, which is rarely going to do what you expect.&amp;nbsp; Most of the time, if you're using LAG(), results are much more predictable if you create a variable *un*conditionally (that is, not part of an IF statement / block), then instead use that variable where you're currently using LAG in your code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
lag_val = lag(val);
if x&amp;gt;y then newval=lag_val;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Dec 2024 01:01:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Invalid-Records-Using-Data-from-Another-Record/m-p/952688#M372309</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2024-12-06T01:01:45Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing Invalid Records Using Data from Another Record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-Invalid-Records-Using-Data-from-Another-Record/m-p/952703#M372314</link>
      <description>&lt;P&gt;Please show some sample data, in the form of a working DATA step.&amp;nbsp; Then show us what you want the result of that sample to look like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Help us help you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Dec 2024 02:38:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-Invalid-Records-Using-Data-from-Another-Record/m-p/952703#M372314</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-12-06T02:38:26Z</dc:date>
    </item>
  </channel>
</rss>

