<?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: Query Explanation needed in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328957#M73530</link>
    <description>&lt;P&gt;Not in front of a computer but my guess is the SQL version is different?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 01 Feb 2017 02:37:44 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-02-01T02:37:44Z</dc:date>
    <item>
      <title>Query Explanation needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328948#M73523</link>
      <description>&lt;P&gt;Hi Team,&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Can some one please help me interpret what this below query means?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC sql;&lt;/P&gt;&lt;P&gt;Create table tmp as&amp;nbsp;&lt;/P&gt;&lt;P&gt;select mbr_id, CASE WHEN qualifier='2' THEN COALESCE(B.ICD9,' ') ELSE A.DX END AS DX&lt;/P&gt;&lt;P&gt;from INP_1 A&lt;/P&gt;&lt;P&gt;LEFT JOIN&lt;/P&gt;&lt;P&gt;testtable B&lt;/P&gt;&lt;P&gt;on A.DX=B.ICD10&amp;nbsp;&lt;/P&gt;&lt;P&gt;WHERE COALESCE(B.smm,0)=0&lt;/P&gt;&lt;P&gt;group by mbr_ID,calculated DX&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Feb 2017 01:48:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328948#M73523</guid>
      <dc:creator>srdhoble</dc:creator>
      <dc:date>2017-02-01T01:48:14Z</dc:date>
    </item>
    <item>
      <title>Re: Query Explanation needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328949#M73524</link>
      <description>&lt;P&gt;It's essentially a lookup from one table to another, and creating a new DX variable. If the qualifier is 2, DX comes from table B, otherwise it's from table A.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is is there a specific issue you're having?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Feb 2017 01:55:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328949#M73524</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-02-01T01:55:39Z</dc:date>
    </item>
    <item>
      <title>Re: Query Explanation needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328950#M73525</link>
      <description>&lt;P&gt;I am getting very few values(like 15-16 values in total) under DX column rest all are comming blanck.I was expecting many hundreds of values under DX column&lt;/P&gt;</description>
      <pubDate>Wed, 01 Feb 2017 01:59:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328950#M73525</guid>
      <dc:creator>srdhoble</dc:creator>
      <dc:date>2017-02-01T01:59:41Z</dc:date>
    </item>
    <item>
      <title>Re: Query Explanation needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328951#M73526</link>
      <description>&lt;P&gt;It's not very well formed, but I'll give it ago. (Translated: I wouldn't have written it quite like that)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your master table, inp_1, contains at least three columns:&amp;nbsp;&lt;EM&gt;mbr_id&lt;/EM&gt;,&amp;nbsp;&lt;EM&gt;qualifier&lt;/EM&gt; and&amp;nbsp;&lt;EM&gt;dx&lt;/EM&gt;. It is being joined with&amp;nbsp;&lt;EM&gt;testtable&lt;/EM&gt; (I hate tables called that - sorry if it's yours, but I really do!) which contains&amp;nbsp;&lt;EM&gt;icd10&lt;/EM&gt;,&amp;nbsp;&lt;EM&gt;icd9&lt;/EM&gt; and&amp;nbsp;&lt;EM&gt;smm.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The target table will contain&amp;nbsp;&lt;EM&gt;inp_1&lt;/EM&gt;'s&amp;nbsp;&lt;EM&gt;mbr_id&lt;/EM&gt; and either&amp;nbsp;&lt;EM&gt;icd_9&amp;nbsp;&lt;/EM&gt;or space (where&amp;nbsp;&lt;EM&gt;icd_9&amp;nbsp;&lt;/EM&gt;is missing or the join failed) if&amp;nbsp;&lt;EM&gt;qualifier&lt;/EM&gt; is&amp;nbsp;&lt;EM&gt;2,&amp;nbsp;&lt;/EM&gt;or&amp;nbsp;&lt;EM&gt;dx&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The&amp;nbsp;&lt;EM&gt;where&lt;/EM&gt; clause is possibly problematic. If a join against&amp;nbsp;&lt;EM&gt;testtable&lt;/EM&gt; can't be found, it will always be true because of the&amp;nbsp;&lt;EM&gt;coalesce&lt;/EM&gt;:&amp;nbsp;&lt;EM&gt;smm&lt;/EM&gt; will be missing, so&amp;nbsp;the function will return&amp;nbsp;&lt;EM&gt;0.&amp;nbsp;&lt;/EM&gt;However if the join is successful and&amp;nbsp;&lt;EM&gt;smm&lt;/EM&gt; is still missing&lt;EM&gt;,&amp;nbsp;&lt;/EM&gt;again it will be true.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The&amp;nbsp;&lt;EM&gt;group by&lt;/EM&gt; is wrong too: there's so grouping of variables. Turn that into an&amp;nbsp;&lt;EM&gt;order by&lt;/EM&gt;. The SQL processor will return a warning message to this effect.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But without seeing the data, I can only ever guess.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Feb 2017 02:05:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328951#M73526</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2017-02-01T02:05:16Z</dc:date>
    </item>
    <item>
      <title>Re: Query Explanation needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328952#M73527</link>
      <description>&lt;P&gt;Also,Can you please help me explain below two commands&lt;/P&gt;&lt;P&gt;Coalesce(B.ICD9, ' ')&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Coalesce(B.SMM,0)=0&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Feb 2017 02:02:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328952#M73527</guid>
      <dc:creator>srdhoble</dc:creator>
      <dc:date>2017-02-01T02:02:12Z</dc:date>
    </item>
    <item>
      <title>Re: Query Explanation needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328954#M73528</link>
      <description>&lt;P&gt;The first one shouldn't work actually, should be COALESCEC (extra C)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From the ever so helpful documentation&lt;/P&gt;
&lt;P&gt;Coalesce -&amp;gt; Returns the first nonmissing value from a list of numeric arguments.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#p1vjttz6nuankzn1gh4z3wgcu0bf.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#p1vjttz6nuankzn1gh4z3wgcu0bf.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Feb 2017 02:14:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328954#M73528</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-02-01T02:14:21Z</dc:date>
    </item>
    <item>
      <title>Re: Query Explanation needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328956#M73529</link>
      <description>&lt;P&gt;Oh it works fine, but I agree, it should be&amp;nbsp;&lt;EM&gt;coalescec&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 62         proc sql;
 63         create table class_coalesce as
 64            select coalesce(name, 'xxx') as name length=30
 65              from sashelp.class;
 NOTE: Table WORK.CLASS_COALESCE created, with 19 rows and 1 columns.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Feb 2017 02:18:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328956#M73529</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2017-02-01T02:18:05Z</dc:date>
    </item>
    <item>
      <title>Re: Query Explanation needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328957#M73530</link>
      <description>&lt;P&gt;Not in front of a computer but my guess is the SQL version is different?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Feb 2017 02:37:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328957#M73530</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-02-01T02:37:44Z</dc:date>
    </item>
    <item>
      <title>Re: Query Explanation needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328959#M73532</link>
      <description>Nah - data step the same. I like to be explicit, because it makes the code less ambiguous. But it's not necessary. It doesn't work the other way though, of course!</description>
      <pubDate>Wed, 01 Feb 2017 02:42:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328959#M73532</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2017-02-01T02:42:09Z</dc:date>
    </item>
    <item>
      <title>Re: Query Explanation needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328962#M73533</link>
      <description>&lt;P&gt;I don't think that's true, but also realizing that using COALESCE with a missing argument doesn't make sense.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tested COALESCE, in a data step with character variables it does throw errors in the log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class;
 set sashelp.class;

 if _n_ in (5, 10, 15) then
    call missing(sex, age, weight, height);
run;

proc sql ;
 create table want_sql as select age, name, sex, coalesce(age, 999) as n_age, 
    coalesce(sex, 'U') as c_sex from class;
quit;

proc print data=want_sql;
run;

data want_ds;
 set class;
 n_age=coalesce(age, 999);
 c_sex=coalesce(sex, 'U');
run;

proc print data=want_ds;
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;</description>
      <pubDate>Wed, 01 Feb 2017 03:02:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328962#M73533</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-02-01T03:02:44Z</dc:date>
    </item>
    <item>
      <title>Re: Query Explanation needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328973#M73536</link>
      <description>&lt;P&gt;Oh **bleep** - I misremembered. I apologise.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Feb 2017 04:21:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328973#M73536</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2017-02-01T04:21:51Z</dc:date>
    </item>
    <item>
      <title>Re: Query Explanation needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328974#M73537</link>
      <description>&lt;P&gt;I was trying to say d__n, by the way, in case you thought I was aiming for something stronger.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Feb 2017 04:24:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/328974#M73537</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2017-02-01T04:24:58Z</dc:date>
    </item>
    <item>
      <title>Re: Query Explanation needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/329011#M73548</link>
      <description>&lt;P&gt;Not sure thats right on the coalesce. &amp;nbsp;Coalesce is an SQL function, returns the first non-missing of a list of variables irrespective of their data type. &amp;nbsp;So in proc sql that shoudl work fine.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Feb 2017 09:31:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/329011#M73548</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-02-01T09:31:19Z</dc:date>
    </item>
    <item>
      <title>Re: Query Explanation needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/329014#M73549</link>
      <description>&lt;P&gt;First, write the code in a nicely formatted manner. &amp;nbsp;This will help you, and others read your code and understand more easily:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table TMP as
  /* Selection phase.  Details what columns to have in the output table */
  select  MBR_ID,
          /* If condition is true then take B.ICD9, otherwise take missing - which is nonsense */ 
          case  when QUALIFIER='2' then coalesce(B.ICD9,' ') 
                else A.DX end as DX
  /* Source sections.  Details what tables to take data from */
  from  INP_1 A
  left join TESTTABLE B
  on    A.DX=B.ICD10 
  /* This section filters so only data where B.SMM is 0 or missing */
  where coalesce(B.SMM,0)=0
  /* Group/order - this section is irrelevant as no data grouping or aggregation is done in selection */
  group by MBR_ID,
           CALCULATED DX;
quit;&lt;/PRE&gt;
&lt;P&gt;So to re-write the query:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table TMP as
  select  MBR_ID,
          case  when QUALIFIER='2' then B.ICD9
                else A.DX end as DX
  from  INP_1 A
  left join (select * from TESTTABLE where SMM &amp;gt; 0) B
  on    A.DX=B.ICD10; 
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Feb 2017 09:41:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-Explanation-needed/m-p/329014#M73549</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-02-01T09:41:06Z</dc:date>
    </item>
  </channel>
</rss>

