<?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: Sql join / key variables already exists on file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/371893#M88851</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi Kurt,&lt;/P&gt;&lt;P&gt;So you suggest complete iteration of all variables I want as shown in below code which worked out without warning. I like the way you put it. However, in my actual data I have group variables such as for instance one of them is "other_diagnosis1-other_diagnosis_25" and also other is "procedure_diagnosis1-procedure15". I need both group variables which i will use in array to look up for a select cases of diseases for the next step. They're both character variables because ICD10 uses letter initials.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you know how to list these two group variables in the sql together?&lt;/P&gt;&lt;P&gt;I tried simply putting "&lt;SPAN&gt;other_diagnosis1-other_diagnosis_25,&amp;nbsp;procedure_diagnosis1-procedure15" which had failed.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
CREATE TABLE FULL_COAL AS
SELECT COALESCE(A.PATIENT, B.PATIENT) AS PATIENT, 
COALESCE(A.MEDCODE, B.MEDCODE) AS MEDCODE,
COALESCE(A.ID, B.ID) AS ID, A.DOSE_ID, A.DOSEAMT, A.DOSEFRQ, B.EFFIC_ID, B.VISIT, B.SCORE
FROM DOSING A FULL JOIN EFFICACY B
ON A.PATIENT = B.PATIENT and 
A.MEDCODE = B.MEDCODE and
A.ID = B.ID;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 29 Jun 2017 20:32:37 GMT</pubDate>
    <dc:creator>Cruise</dc:creator>
    <dc:date>2017-06-29T20:32:37Z</dc:date>
    <item>
      <title>Sql join / key variables already exists on file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/371492#M88745</link>
      <description>&lt;P&gt;Shortcut with alias: A.*, B.* instigates below&amp;nbsp;warnings. Is it dangerous to ignore this warning? In this simple datasets of demo I see no harm checking in the resulting data. But it will be harder to check in my actual where my datasets have hundreds of variables and 4 key vars for full outer join.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;WARNING: Variable patient already exists on file WORK.FULL_COAL.&lt;BR /&gt;WARNING: Variable medcode already exists on file WORK.FULL_COAL.&lt;BR /&gt;WARNING: Variable ID already exists on file WORK.FULL_COAL.&lt;BR /&gt;WARNING: Variable PATIENT already exists on file WORK.FULL_COAL.&lt;BR /&gt;WARNING: Variable medcode already exists on file WORK.FULL_COAL.&lt;BR /&gt;WARNING: Variable ID already exists on file WORK.FULL_COAL.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ideally,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;PROC SQL;&lt;BR /&gt;CREATE TABLE FULL_COAL AS&lt;BR /&gt;SELECT COALESCE(A.PATIENT, B.PATIENT) AS PATIENT, &lt;BR /&gt; COALESCE(A.MEDCODE, B.MEDCODE)       AS MEDCODE,&lt;BR /&gt; COALESCE(A.ID, B.ID)                 AS ID, A.*, B.*&lt;BR /&gt;FROM DOSING A FULL JOIN EFFICACY B&lt;BR /&gt;ON A.PATIENT = B.PATIENT and &lt;BR /&gt;   A.MEDCODE = B.MEDCODE and&lt;BR /&gt;   A.ID = B.ID;&lt;BR /&gt;QUIT;&lt;/PRE&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Reference: used demo code and data appeared in SUGI paper:&amp;nbsp;&lt;A href="http://www2.sas.com/proceedings/sugi25/25/cc/25p109.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi25/25/cc/25p109.pdf&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jun 2017 20:20:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/371492#M88745</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2017-06-29T20:20:05Z</dc:date>
    </item>
    <item>
      <title>Re: Sql join / key variables already exists on file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/371500#M88752</link>
      <description>&lt;P&gt;IMO you should not have these warnings. If the variable is in both datasets are you 100% sure that the data is the same for every record you've joined on? If not how do you know which is correct. It's best to select your data directly and if you have variables with the same name you need to correct for that.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jun 2017 00:33:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/371500#M88752</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-06-29T00:33:42Z</dc:date>
    </item>
    <item>
      <title>Re: Sql join / key variables already exists on file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/371502#M88754</link>
      <description>&lt;P&gt;If you are rock solid sure of what SQL will do (and I'm not, I would have to test it which I can't do right now), you might be able to ignore the warnings. &amp;nbsp;First, consider where the warnings&amp;nbsp;come from.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are creating three MEDCODE variables. &amp;nbsp;One is calculated using COALESCE, one comes from A, and one comes from B. &amp;nbsp;Which one is supposed to be included in your output table? &amp;nbsp;Could the value for MEDCODE actually come from B, when A contains a different value? &amp;nbsp;Could that happen when B contains a missing value?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Most likely, you just have to remove A.* and B.* from the SELECT statement. &amp;nbsp;COALESCE can still work on the values from A and B, even if those values are not being selected. &amp;nbsp;Are there any variables in A or B that you need to read in, that are not going to be used in a COALESCE function?&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jun 2017 00:49:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/371502#M88754</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-06-29T00:49:55Z</dc:date>
    </item>
    <item>
      <title>Re: Sql join / key variables already exists on file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/371544#M88770</link>
      <description>&lt;P&gt;Don't make indiscriminate use of the asterisk in SQL select lists, especially not when you're joining&lt;/P&gt;
&lt;P&gt;- it causes the problems you just experience, and the result might be unpredictable (which value ends up in the output).&lt;/P&gt;
&lt;P&gt;- in &amp;gt; 90% of cases you do not need all columns in your further analysis, so you waste disk space and therefore processing time.&lt;/P&gt;
&lt;P&gt;- an unexpected change in the input datasets might go undetected in this step, causing havoc later on. Makes code less maintainable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maxim 25: Have a clean log.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jun 2017 06:45:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/371544#M88770</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-29T06:45:25Z</dc:date>
    </item>
    <item>
      <title>Re: Sql join / key variables already exists on file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/371893#M88851</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi Kurt,&lt;/P&gt;&lt;P&gt;So you suggest complete iteration of all variables I want as shown in below code which worked out without warning. I like the way you put it. However, in my actual data I have group variables such as for instance one of them is "other_diagnosis1-other_diagnosis_25" and also other is "procedure_diagnosis1-procedure15". I need both group variables which i will use in array to look up for a select cases of diseases for the next step. They're both character variables because ICD10 uses letter initials.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you know how to list these two group variables in the sql together?&lt;/P&gt;&lt;P&gt;I tried simply putting "&lt;SPAN&gt;other_diagnosis1-other_diagnosis_25,&amp;nbsp;procedure_diagnosis1-procedure15" which had failed.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
CREATE TABLE FULL_COAL AS
SELECT COALESCE(A.PATIENT, B.PATIENT) AS PATIENT, 
COALESCE(A.MEDCODE, B.MEDCODE) AS MEDCODE,
COALESCE(A.ID, B.ID) AS ID, A.DOSE_ID, A.DOSEAMT, A.DOSEFRQ, B.EFFIC_ID, B.VISIT, B.SCORE
FROM DOSING A FULL JOIN EFFICACY B
ON A.PATIENT = B.PATIENT and 
A.MEDCODE = B.MEDCODE and
A.ID = B.ID;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jun 2017 20:32:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/371893#M88851</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2017-06-29T20:32:37Z</dc:date>
    </item>
    <item>
      <title>Re: Sql join / key variables already exists on file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/371902#M88856</link>
      <description>&lt;P&gt;Here's a quick trick that will work but is a bit tedious, use the FEEDBACK option. Note how the alias works. Anyways, look at the log and you'll see the full code and can copy and paste that into your program and remove the variables you don't need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql FEEDBACK;
create table want as
select *
from sashelp.class as SC;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jun 2017 20:50:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/371902#M88856</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-06-29T20:50:07Z</dc:date>
    </item>
    <item>
      <title>Re: Sql join / key variables already exists on file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/371908#M88860</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&lt;/P&gt;&lt;P&gt;Thanks Reeza,&lt;/P&gt;&lt;P&gt;How would happen to&amp;nbsp;COALESCE option then? I'd like to coalesce because I have to track the origin of missing in the final output by key variables (patient, medcode and id in this demo)?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dosing;
input patient medcode $ dose_id doseamt dosefrq ID;
datalines;
1001    A      1     2     2 1
1003    A      2     1     2 2
1004    A      3     1     2 3
1004    B      4     4     2 4
1006    B      5     2     2 5
1007    A      6     2     1 6
1008    A      7     1     2 1
1009    A      8     2     2 1
;
run;
data efficacy;
input PATIENT medcode $ EFFIC_ID VISIT SCORE ID;
datalines;
1001 A 1 1 4 1
1002 A 2 1 5 2
1004 A 3 1 2 3
1004 A 4 2 1 4
1005 A 5 1 2 1
1009 A 6 1 5 0
;
run;&lt;BR /&gt;&lt;BR /&gt;PROC SQL FEEDBACK;&lt;BR /&gt;CREATE TABLE FULL_COAL AS&lt;BR /&gt;SELECT COALESCE(A.PATIENT, B.PATIENT) AS PATIENT, &lt;BR /&gt;       COALESCE(A.MEDCODE, B.MEDCODE) AS MEDCODE,&lt;BR /&gt;       COALESCE(A.ID,      B.ID)      AS ID............????&lt;BR /&gt;FROM DOSING A FULL JOIN EFFICACY B&lt;BR /&gt; ON A.PATIENT = B.PATIENT and &lt;BR /&gt;    A.MEDCODE = B.MEDCODE and&lt;BR /&gt;    A.ID = B.ID;&lt;BR /&gt;QUIT;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HH&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jun 2017 20:59:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/371908#M88860</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2017-06-29T20:59:45Z</dc:date>
    </item>
    <item>
      <title>Re: Sql join / key variables already exists on file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/371918#M88867</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;@SUNY_Maggie wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Thanks Reeza,&lt;/P&gt;
&lt;P&gt;How would happen to&amp;nbsp;COALESCE option then? I'd like to coalesce because I have to track the origin of missing in the final output by key variables (patient, medcode and id in this demo)?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dosing;
input patient medcode $ dose_id doseamt dosefrq ID;
datalines;
1001    A      1     2     2 1
1003    A      2     1     2 2
1004    A      3     1     2 3
1004    B      4     4     2 4
1006    B      5     2     2 5
1007    A      6     2     1 6
1008    A      7     1     2 1
1009    A      8     2     2 1
;
run;
data efficacy;
input PATIENT medcode $ EFFIC_ID VISIT SCORE ID;
datalines;
1001 A 1 1 4 1
1002 A 2 1 5 2
1004 A 3 1 2 3
1004 A 4 2 1 4
1005 A 5 1 2 1
1009 A 6 1 5 0
;
run;&lt;BR /&gt;&lt;BR /&gt;PROC SQL FEEDBACK;&lt;BR /&gt;CREATE TABLE FULL_COAL AS&lt;BR /&gt;SELECT COALESCE(A.PATIENT, B.PATIENT) AS PATIENT, &lt;BR /&gt;       COALESCE(A.MEDCODE, B.MEDCODE) AS MEDCODE,&lt;BR /&gt;       COALESCE(A.ID,      B.ID)      AS ID............????&lt;BR /&gt;FROM DOSING A FULL JOIN EFFICACY B&lt;BR /&gt; ON A.PATIENT = B.PATIENT and &lt;BR /&gt;    A.MEDCODE = B.MEDCODE and&lt;BR /&gt;    A.ID = B.ID;&lt;BR /&gt;QUIT;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HH&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Run it first with your original code that generated the Warning.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Copy the code from the log and remove the duplicate fields. The reason you're having issues is because you create a variable called PATIENT and also selected both variables called PATIENT from the datasets A/B.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, Since your code joins on the same fields you're using in COALESCE you have a major flaw in your logic. The COALESCE will do nothing because the IDs already match given your join condition.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jun 2017 21:51:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/371918#M88867</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-06-29T21:51:40Z</dc:date>
    </item>
    <item>
      <title>Re: Sql join / key variables already exists on file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/371979#M88894</link>
      <description>&lt;P&gt;You can spell out the variables you want to keep instead of using *.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;CREATE TABLE FULL_COAL AS
  SELECT COALESCE(A.PATIENT, B.PATIENT) AS PATIENT
       , COALESCE(A.MEDCODE, B.MEDCODE) AS MEDCODE
       , COALESCE(A.ID, B.ID) AS ID
       , A.other1
       , B.other2
  FROM DOSING A
  FULL JOIN EFFICACY B
    ON A.PATIENT = B.PATIENT
   AND A.MEDCODE = B.MEDCODE
   AND A.ID = B.ID
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can use RENAME= dataset option on the input datasets and DROP= option on the output dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;CREATE TABLE FULL_COAL(drop=a1 a2 a3 b1 b2 b3) AS
  SELECT COALESCE(A.a1, B.b1) AS PATIENT
       , COALESCE(A.a2, B.b2) AS MEDCODE
       , COALESCE(A.a3, B.b3) AS ID
       , *
  FROM DOSING(rename=(patient=a1 medcode=a2 id=a3)) A
  FULL JOIN EFFICACY(rename=(patient=b1 medcode=b2 id=b3)) B
    ON A.A1 = B.B1
   AND A.A2 = B.B2
   AND A.A3 = B.B3
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you know that only the key variables are common you can use NATURAL join.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;CREATE TABLE FULL_COAL AS
  SELECT *
  FROM DOSING A
  NATURAL FULL JOIN EFFICACY B
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Jun 2017 03:31:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/371979#M88894</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-06-30T03:31:02Z</dc:date>
    </item>
    <item>
      <title>Re: Sql join / key variables already exists on file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/372014#M88901</link>
      <description>&lt;P&gt;If you do not have a many-to-many relationship between the tables, then sorting and using a data step merge would be the better solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=dosing;
by patient medcode id;
run;

proc sort data=efficacy;
by patient medcode id;
run;

data full;
merge
  dosing
  efficacy
;
by patient medcode id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since the WARNINGs from your original code show that only the key variables patient, mecode and id are on both datasets, this will work without unintended side effects and WARNINGs.&lt;/P&gt;
&lt;P&gt;One really needs SQL only when there is a many-to-many relationship and a cartesian product has to be built.&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jun 2017 06:27:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sql-join-key-variables-already-exists-on-file/m-p/372014#M88901</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-30T06:27:10Z</dc:date>
    </item>
  </channel>
</rss>

