<?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 trace source datasets in the proc sql output? in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/How-to-trace-source-datasets-in-the-proc-sql-output/m-p/416800#M67646</link>
    <description>&lt;P&gt;Summarizing from responses:&lt;/P&gt;&lt;P&gt;proc sql:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table sharp as
 select a.id as id_a,b.id as id_b,
 a.latitude as lat_a,b.latitude as lat_b
  from data1 as a full join data2 as b
   on a.id=b.id;
quit;

data sharp1; set sharp;
length source $ 9;
if lat_a ^=. then source='a'; 
if lat_b ^=. then source='b';
if lat_a =. and lat_b=. then source='both_miss';
if lat_a ^=. and lat_b^=. then source='match';
run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=first;&lt;BR /&gt;by uid;&lt;BR /&gt;proc sort data=second;&lt;BR /&gt;by uid;&lt;BR /&gt;data check;&lt;BR /&gt;&amp;nbsp;merge first &lt;BR /&gt;(in=ina rename=(latitude=lat_a)) &lt;BR /&gt;second(in=inb rename=(latitude=lat_b));&lt;BR /&gt;&amp;nbsp;by uid;&lt;BR /&gt;&amp;nbsp;if ina then id_a=uid;&lt;BR /&gt;&amp;nbsp;if inb then id_b=uid;&lt;BR /&gt;&amp;nbsp;drop uid;&lt;BR /&gt;&amp;nbsp;if lat_a=. then first=.;&lt;BR /&gt;&amp;nbsp;if lat_b=. then second=.;&lt;BR /&gt;&amp;nbsp;if lat_a^=. then first=1;&lt;BR /&gt;&amp;nbsp;if lat_b^=. then second=1;&lt;BR /&gt;run;&lt;BR /&gt;proc freq data=m.check;&lt;BR /&gt;tables first*second;&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 28 Nov 2017 19:20:54 GMT</pubDate>
    <dc:creator>Cruise</dc:creator>
    <dc:date>2017-11-28T19:20:54Z</dc:date>
    <item>
      <title>How to trace source datasets in the proc sql output?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-trace-source-datasets-in-the-proc-sql-output/m-p/416516#M67622</link>
      <description>&lt;P&gt;How to trace source datasets in the proc sql output?&lt;/P&gt;
&lt;P&gt;My goal is to figure out the contribution of missings, say, for variable "latitude". I'm a novice to CASE WHEN and COALESCE functions in proc sql. Therefore, proc sql code below is to rather explain how I envisioned what might have worked out. There are nice SUGI papers but I didn't find one that discussed about indicator variable in "both_missing" scenario.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data1;
	input id latitude;
	cards;
1001 43.2
1003 43.6
1004 .
;

DATA data2;
	INPUT id latitude;
	CARDS;
1001 43.2
1002 48.3
1004 .
;

data want;
	input id_a id_b lat_a lat_b indicator $;
	cards;
1001 1001 43.2 43.2	match
.	 1002 .    48.3	a_miss
1003 .	  43.6 .	b_miss
1004 1004 .    .	both_miss
;

PROC SQL;
	CREATE TABLE want AS
		SELECT a.id as id_a,
			a.latitude as lat_a,
			b.id as id_b,
			b.latitude as lat_b,
		CASE (a.latitude = b.latitude)
			WHEN lat_a=lat_b THEN 'Match'
			WHEN lat_a=. THEN 'a_miss'  
			WHEN lat_b=. THEN 'b_miss'  
			WHEN lat_a=. and lat_b=. THEN 'both_miss' 
			ELSE 'else'
		END 
	AS indvar LENGTH=5
		FROM data1 a join data2 b
			ON a.id = b.id;
QUIT;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Nov 2017 22:34:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-trace-source-datasets-in-the-proc-sql-output/m-p/416516#M67622</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2017-11-27T22:34:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to trace source datasets in the proc sql output?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-trace-source-datasets-in-the-proc-sql-output/m-p/416523#M67623</link>
      <description>&lt;P&gt;Are you trying to replicate the IN functionality of a data step merge?&lt;/P&gt;</description>
      <pubDate>Mon, 27 Nov 2017 22:33:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-trace-source-datasets-in-the-proc-sql-output/m-p/416523#M67623</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-11-27T22:33:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to trace source datasets in the proc sql output?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-trace-source-datasets-in-the-proc-sql-output/m-p/416634#M67629</link>
      <description>&lt;P&gt;The current syntax is incorrect, the condition immediately after CASE&amp;nbsp;needs to be removed.&lt;/P&gt;
&lt;P&gt;Also, SQL evaluates WHEN in sequence, therefore you need to move the "both" upwards in the code.&lt;/P&gt;
&lt;P&gt;Also, I think you need to define lat_a given your input program.&lt;/P&gt;
&lt;P&gt;But haven't you test your code? Do that and return with a more specific question...&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2017 10:58:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-trace-source-datasets-in-the-proc-sql-output/m-p/416634#M67629</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2017-11-28T10:58:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to trace source datasets in the proc sql output?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-trace-source-datasets-in-the-proc-sql-output/m-p/416676#M67631</link>
      <description>&lt;P&gt;It is very easy for Data Step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data1;
	input id latitude;
	cards;
1001 43.2
1003 43.6
1004 .
;

DATA data2;
	INPUT id latitude;
	CARDS;
1001 43.2
1002 48.3
1004 .
;

data want;
 merge data1(in=ina rename=(latitude=latitude_a)) 
 data2(in=inb rename=(latitude=latitude_b));
 by id;
 if ina then id_a=id;
 if inb then id_b=id;
 drop id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Nov 2017 13:37:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-trace-source-datasets-in-the-proc-sql-output/m-p/416676#M67631</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-11-28T13:37:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to trace source datasets in the proc sql output?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-trace-source-datasets-in-the-proc-sql-output/m-p/416677#M67632</link>
      <description>I have achieved it in data step. I was wondering whether proc sql has a way to achieve the result similarly?</description>
      <pubDate>Tue, 28 Nov 2017 13:39:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-trace-source-datasets-in-the-proc-sql-output/m-p/416677#M67632</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2017-11-28T13:39:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to trace source datasets in the proc sql output?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-trace-source-datasets-in-the-proc-sql-output/m-p/416680#M67633</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data1;
	input id latitude;
	cards;
1001 43.2
1003 43.6
1004 .
;

DATA data2;
	INPUT id latitude;
	CARDS;
1001 43.2
1002 48.3
1004 .
;

proc sql;
create table want as
 select a.id as id_a,b.id as id_b,
 a.latitude as lat_a,b.latitude as lat_b
  from data1 as a full join data2 as b
   on a.id=b.id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Nov 2017 13:46:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-trace-source-datasets-in-the-proc-sql-output/m-p/416680#M67633</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-11-28T13:46:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to trace source datasets in the proc sql output?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-trace-source-datasets-in-the-proc-sql-output/m-p/416800#M67646</link>
      <description>&lt;P&gt;Summarizing from responses:&lt;/P&gt;&lt;P&gt;proc sql:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table sharp as
 select a.id as id_a,b.id as id_b,
 a.latitude as lat_a,b.latitude as lat_b
  from data1 as a full join data2 as b
   on a.id=b.id;
quit;

data sharp1; set sharp;
length source $ 9;
if lat_a ^=. then source='a'; 
if lat_b ^=. then source='b';
if lat_a =. and lat_b=. then source='both_miss';
if lat_a ^=. and lat_b^=. then source='match';
run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=first;&lt;BR /&gt;by uid;&lt;BR /&gt;proc sort data=second;&lt;BR /&gt;by uid;&lt;BR /&gt;data check;&lt;BR /&gt;&amp;nbsp;merge first &lt;BR /&gt;(in=ina rename=(latitude=lat_a)) &lt;BR /&gt;second(in=inb rename=(latitude=lat_b));&lt;BR /&gt;&amp;nbsp;by uid;&lt;BR /&gt;&amp;nbsp;if ina then id_a=uid;&lt;BR /&gt;&amp;nbsp;if inb then id_b=uid;&lt;BR /&gt;&amp;nbsp;drop uid;&lt;BR /&gt;&amp;nbsp;if lat_a=. then first=.;&lt;BR /&gt;&amp;nbsp;if lat_b=. then second=.;&lt;BR /&gt;&amp;nbsp;if lat_a^=. then first=1;&lt;BR /&gt;&amp;nbsp;if lat_b^=. then second=1;&lt;BR /&gt;run;&lt;BR /&gt;proc freq data=m.check;&lt;BR /&gt;tables first*second;&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2017 19:20:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-trace-source-datasets-in-the-proc-sql-output/m-p/416800#M67646</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2017-11-28T19:20:54Z</dc:date>
    </item>
  </channel>
</rss>

