<?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: Adding a variable from another table based on a condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-from-another-table-based-on-a-condition/m-p/854508#M337708</link>
    <description>Thank you! This also worked!</description>
    <pubDate>Thu, 19 Jan 2023 08:09:22 GMT</pubDate>
    <dc:creator>AmirSari</dc:creator>
    <dc:date>2023-01-19T08:09:22Z</dc:date>
    <item>
      <title>Adding a variable from another table based on a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-from-another-table-based-on-a-condition/m-p/854476#M337690</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to add column Y from table B to table A if, for each ID, the values in column Y are right after or before the values of column X in table A.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;table A:&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; X&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;6&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp;11&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;table B:&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; Y&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;11&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;16&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;8&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp;7&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp;9&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp;14&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp;13&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I need:&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; X&amp;nbsp; &amp;nbsp; &amp;nbsp;Y&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;9&amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;9&amp;nbsp; &amp;nbsp; 11&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;6&amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;6&amp;nbsp; &amp;nbsp; &amp;nbsp;8&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp;11&amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp;11&amp;nbsp; &amp;nbsp; &amp;nbsp;13&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2023 01:46:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-from-another-table-based-on-a-condition/m-p/854476#M337690</guid>
      <dc:creator>AmirSari</dc:creator>
      <dc:date>2023-01-19T01:46:02Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable from another table based on a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-from-another-table-based-on-a-condition/m-p/854486#M337699</link>
      <description>&lt;P&gt;You could probably get away with interleaving the two datasets.&lt;/P&gt;
&lt;P&gt;First let's convert your example listings into actual datasets.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
  input id x;
cards;
1  9
2  6
3 11
;
data b;
  input id y ;
cards;
1 4
1 3
1 7
1 11
1 16
2 1
2 8
2 5
2 9
3 7
3 9
3 14
3 13
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For the interleaving to work they need to be sorted.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=a;
  by id x;
run;
proc sort data=b;
  by id y;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now since you want to interleave by both the ID and the other variable you will have rename on on the way in.&lt;/P&gt;
&lt;P&gt;To find the previous value use LAG().&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set b(in=inb rename=(y=x)) a(in=ina);
  by id x;
  lagx=lag(x);
  if first.id then lagx=.;
  if ina then y=lagx;
  if inb and not first.id and lag(ina)=1 then do; y=x; x=lagx; end;
  if not missing(y);
  drop lagx;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Jan 2023 04:43:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-from-another-table-based-on-a-condition/m-p/854486#M337699</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-01-19T04:43:08Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable from another table based on a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-from-another-table-based-on-a-condition/m-p/854497#M337704</link>
      <description>&lt;P&gt;Just an idea:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data distance;
   merge two one;
   by Id;
   
   d = x - y;
   s = sign(d);
   d = abs(d);
run;

proc sort data=distance out=sorted;
   by id s d;
run;

data want;
   set sorted;
   by id s;
   
   if first.s;
   
   drop s d;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Jan 2023 06:29:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-from-another-table-based-on-a-condition/m-p/854497#M337704</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2023-01-19T06:29:16Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a variable from another table based on a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-from-another-table-based-on-a-condition/m-p/854508#M337708</link>
      <description>Thank you! This also worked!</description>
      <pubDate>Thu, 19 Jan 2023 08:09:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-a-variable-from-another-table-based-on-a-condition/m-p/854508#M337708</guid>
      <dc:creator>AmirSari</dc:creator>
      <dc:date>2023-01-19T08:09:22Z</dc:date>
    </item>
  </channel>
</rss>

