<?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: Using Coalesce function.  Output which table the value came from? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-function-Output-which-table-the-value-came-from/m-p/794549#M254778</link>
    <description>&lt;P&gt;You could use data step UPDATE statement instead.&lt;/P&gt;
&lt;P&gt;Let's create some test datasets.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do id=1 to 5; output; end;
run;

data test1;
  input id value;
cards;
1 .
2 4
;

data test2;
  input id value;
cards;
1 3
2 5
3 .
;
data test3;
  input id value;
cards;
1 6
2 7
3 8
4 .
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now stack the "test" data together into a transaction dataset. Use the INDSNAME= option of the SET statement to find the name of that dataset that is supplying each record.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data trans;
  set test1-test3 indsname=indsname;
  by id;
  where not missing(value);
  dsname=indsname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now apply these transactions to you original dataset.&amp;nbsp; Keep track of whether or not a non missing value has been found and only write that observation instead of the normal UPDATE behavior of just writing the final observation for the group.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  update have trans;
  by id;
  if first.id then found=0;
  if not missing(value) and not found then do; found+1; output; end;
  if last.id and not found then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs    id    value      dsname      found

 1      1      3      WORK.TEST2      1
 2      2      4      WORK.TEST1      1
 3      2      5      WORK.TEST2      1
 4      3      .                      0
 5      4      .                      0
 6      5      .                      0
&lt;/PRE&gt;</description>
    <pubDate>Fri, 04 Feb 2022 16:30:03 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-02-04T16:30:03Z</dc:date>
    <item>
      <title>Using Coalesce function.  Output which table the value came from?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-function-Output-which-table-the-value-came-from/m-p/794520#M254765</link>
      <description>&lt;P&gt;I Have code similar to the following, using the coalesce function to join Value1 to taple Temp_Original from 6 possible tables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a way to also have it output which table the value was taken from?&amp;nbsp;&lt;BR /&gt;I could write separate code to check the if the value is missing from each table in sequence, and output the Table name.&amp;nbsp; But I was hoping there might be a smoother way to do it, perhaps using some saved value from the coalesce function indicating the placement of the first non empty value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;any ideas?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;&lt;BR /&gt;create table Temp_Join as&lt;BR /&gt;select A.*, &lt;BR /&gt;Coalesce(B.Value1,C.Value1,D.Value1,E.Value1,F.Value1,G.Value1) as Value1,&lt;BR /&gt;Coalesce(B.Value2,C.Value2,D.Value2,E.Value2,F.Value2,G.Value2) as Value2&lt;BR /&gt;from Temp_Original as A &lt;BR /&gt;left join Temp1 as B on A.SomeValue = B.SomeValue&lt;BR /&gt;left join Temp2 as C on A.SomeValue = C.SomeValue&lt;BR /&gt;left join Temp3 as D on A.SomeValue = D.SomeValue&lt;BR /&gt;left join Temp4 as E on A.SomeValue = E.SomeValue&lt;BR /&gt;left join Temp5 as F on A.SomeValue = F.SomeValue&lt;BR /&gt;left join Temp6 as G on A.SomeValue = G.SomeValue;&lt;BR /&gt;quit;
&amp;nbsp;&lt;/CODE&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Feb 2022 14:09:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-function-Output-which-table-the-value-came-from/m-p/794520#M254765</guid>
      <dc:creator>mcook</dc:creator>
      <dc:date>2022-02-04T14:09:45Z</dc:date>
    </item>
    <item>
      <title>Re: Using Coalesce function.  Output which table the value came from?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-function-Output-which-table-the-value-came-from/m-p/794538#M254771</link>
      <description>&lt;P&gt;No easy way to do this, or none that i know of with Proc SQL . &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is one way that i know of doing this, unfortunately, you didn't provide any sample data so hard to test but here is some code to start you off.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would use the data step to accomplish this task as the data step has much more flexibility and control when processing data&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;/*Make sure all data is sorted in this example its*/
/*by SomeValue */&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;/*Do this for each file - if not all ready sorted*/
/*below is one example*/
/*proc sort data=Temp_original;*/
/*by somevalue;*/
/*run;*/

Data Temp_Join asselect;
	Merge Temp_Original(in=INOriginal)
          Temp1(in=INTemp1)
		  Temp2(in=INTemp2)
		  Temp3(in=INTemp3)
		  Temp4(in=INTemp4)
		  Temp5(in=INTemp5)
		  Temp6(in=INTemp6);
		if INOriginal=1 then fromfile=0; &lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;        if INTemp1=1 then fromfile=1;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;        if INTemp2=1 then fromfile=2;
		if INTemp3=1 then fromfile=3;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;        if INTemp4=1 then fromfile=4;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;        if INTemp5=1 then fromfile=5;
		if INTemp6=1 then fromfile=6;
	if INOriginal=1; /*Keeping all by variables that match the Original file*/
run;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Feb 2022 15:17:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-function-Output-which-table-the-value-came-from/m-p/794538#M254771</guid>
      <dc:creator>CarmineVerrell</dc:creator>
      <dc:date>2022-02-04T15:17:33Z</dc:date>
    </item>
    <item>
      <title>Re: Using Coalesce function.  Output which table the value came from?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-function-Output-which-table-the-value-came-from/m-p/794545#M254776</link>
      <description>&lt;P&gt;Use WHICHN() , or if the values are string WHICHC().&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
create table Temp_Join as
select A.*
     , Coalesce(B.Value1,C.Value1,D.Value1,E.Value1,F.Value1,G.Value1) as Value1
     , whichn(calculated value1,B.Value1,C.Value1,D.Value1,E.Value1,F.Value1,G.Value1) as Which1
from Temp_Original as A 
left join Temp1 as B on A.SomeValue = B.SomeValue
left join Temp2 as C on A.SomeValue = C.SomeValue
left join Temp3 as D on A.SomeValue = D.SomeValue
left join Temp4 as E on A.SomeValue = E.SomeValue
left join Temp5 as F on A.SomeValue = F.SomeValue
left join Temp6 as G on A.SomeValue = G.SomeValue
;
quit;
    &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So WHICH1 will have 1 if the value came from B, 2 if it came from C, etc.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Feb 2022 16:09:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-function-Output-which-table-the-value-came-from/m-p/794545#M254776</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-04T16:09:52Z</dc:date>
    </item>
    <item>
      <title>Re: Using Coalesce function.  Output which table the value came from?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-function-Output-which-table-the-value-came-from/m-p/794549#M254778</link>
      <description>&lt;P&gt;You could use data step UPDATE statement instead.&lt;/P&gt;
&lt;P&gt;Let's create some test datasets.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do id=1 to 5; output; end;
run;

data test1;
  input id value;
cards;
1 .
2 4
;

data test2;
  input id value;
cards;
1 3
2 5
3 .
;
data test3;
  input id value;
cards;
1 6
2 7
3 8
4 .
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now stack the "test" data together into a transaction dataset. Use the INDSNAME= option of the SET statement to find the name of that dataset that is supplying each record.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data trans;
  set test1-test3 indsname=indsname;
  by id;
  where not missing(value);
  dsname=indsname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now apply these transactions to you original dataset.&amp;nbsp; Keep track of whether or not a non missing value has been found and only write that observation instead of the normal UPDATE behavior of just writing the final observation for the group.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  update have trans;
  by id;
  if first.id then found=0;
  if not missing(value) and not found then do; found+1; output; end;
  if last.id and not found then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs    id    value      dsname      found

 1      1      3      WORK.TEST2      1
 2      2      4      WORK.TEST1      1
 3      2      5      WORK.TEST2      1
 4      3      .                      0
 5      4      .                      0
 6      5      .                      0
&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Feb 2022 16:30:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-function-Output-which-table-the-value-came-from/m-p/794549#M254778</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-04T16:30:03Z</dc:date>
    </item>
  </channel>
</rss>

