<?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: Help with the data using  PROC SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-the-data-using-PROC-SQL/m-p/248523#M46690</link>
    <description>&lt;P&gt;last attempt:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input hcp_id :$3. mkt_id :$2. prd_id :$2. mkt_trx1-mkt_trx3 2. prd_trx1-prd_trx3 2.;
datalines;
123 12 4 1 3 2 0 2 1
123 12 5 1 3 2 1 1 1
124 12 1 3 2 1 3 2 1
;

data cmpny_prod;
input mkt_id :$2. prd_id :$2.;
datalines;
12 5
12 8
12 9
;

proc sql;
create table want as 
select b.hcp_id, b.mkt_id, a.prd_id, 
    b.mkt_trx1, b.mkt_trx2, b.mkt_trx3, 
    coalesce(c.prd_trx1, 0) as prd_trx1, 
    coalesce(c.prd_trx2, 0) as prd_trx2,
    coalesce(c.prd_trx3, 0) as prd_trx3
from 
    (   select hcp_id, mkt_id, prd_id from have 
        union 
        select hcp_id, mkt_id, prd_id from (select hcp_id from have), cmpny_prod ) as a inner join 
    (select unique hcp_id, mkt_id, mkt_trx1, mkt_trx2, mkt_trx3 from have) as b 
        on a.hcp_id=b.hcp_id and a.mkt_id=b.mkt_id left join 
    have as c 
        on b.hcp_id=c.hcp_id and b.mkt_id=c.mkt_id and a.prd_id=c.prd_id and 
            b.mkt_trx1=c.mkt_trx1 and b.mkt_trx2=c.mkt_trx2 and b.mkt_trx3=c.mkt_trx3;
select * from want;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 07 Feb 2016 02:31:38 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2016-02-07T02:31:38Z</dc:date>
    <item>
      <title>Help with the data using  PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-the-data-using-PROC-SQL/m-p/248137#M46581</link>
      <description>&lt;DIV class="lia-component-comment-header"&gt;
&lt;DIV class="lia-message-body"&gt;
&lt;DIV class="lia-message-body-content"&gt;
&lt;P&gt;I have following data with company drugs as well as competitors drugs under the market at hcp_id level for 3 months.&lt;/P&gt;
&lt;P&gt;I am showing one market_id and one hcp id as an example.&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input hcp_id $3. mkt_id $2. prd_id $2. mkt_trx1-mktrx3 2. prd_trx1-prd_trx3 2.;&lt;BR /&gt;cards;&lt;BR /&gt;123 12 4 1 3 2 0 2 1&lt;BR /&gt;123 12 5 1 3 2 1 1 1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have list of company product under the market id 12:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data cmpny_prod;&lt;BR /&gt;input mkt_id $2. prd_id $2.;&lt;BR /&gt;cards;&lt;BR /&gt;12 8&lt;BR /&gt;12 9&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;I want the output below that includes prd_id (company products) from list under market 12 (if they are not in the have data) with 0 &lt;BR /&gt;trxs for 3 months under the market id 12 along with other competitors products.&lt;/P&gt;
&lt;P&gt;Output should be as follows:&lt;/P&gt;
&lt;P&gt;hcp_id mkt_id prd_id mkt_trx1 mkt_trx2 mkt_trx3 prd_trx1 prd_trx2 prd_trx3&lt;BR /&gt;123 12 4 1 3 2 0 2 1&lt;BR /&gt;123 12 5 1 3 2 1 1 1&lt;BR /&gt;123 12 8 1 3 2 0 0 0&lt;BR /&gt;123 12 9 1 3 2 0 0 0&lt;/P&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="lia-component-comment-footer"&gt;
&lt;DIV class="lia-message-footer"&gt;
&lt;DIV class="lia-quilt-row lia-quilt-row-standard"&gt;
&lt;DIV class="lia-quilt-column lia-quilt-column-16 lia-quilt-column-left"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="lia-quilt-column lia-quilt-column-16 lia-quilt-column-left"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="lia-quilt-column lia-quilt-column-16 lia-quilt-column-left"&gt;I would appreciate any help in this.. &amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="lia-quilt-column lia-quilt-column-08 lia-quilt-column-right"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Fri, 05 Feb 2016 01:51:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-the-data-using-PROC-SQL/m-p/248137#M46581</guid>
      <dc:creator>pp2014</dc:creator>
      <dc:date>2016-02-05T01:51:38Z</dc:date>
    </item>
    <item>
      <title>Re: Help with the data using  PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-the-data-using-PROC-SQL/m-p/248149#M46585</link>
      <description>&lt;P&gt;Why you need SQL code ? There are many thing you need to consider about . The following can give you a start.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input hcp_id : $3. mkt_id : $2. prd_id :$2. mk_trx1-mk_trx3 2. prd_trx1-prd_trx3 2.;
cards;
123 12 4 1 3 2 0 2 1
123 12 5 1 3 2 1 1 1
;
run;


data cmpny_prod;
input mkt_id :$2. prd_id : $2.;
cards;
12 8
12 9
;
run;
proc sql;
create table want as
 select
  max(hcp_id) as hcp_id,
  mkt_id,
  prd_id,
  max(mk_trx1) as mk_trx1,
  max(mk_trx2) as mk_trx2,
  max(mk_trx3) as mk_trx3,
  coalesce(prd_trx1,0) as prd_trx1,
  coalesce(prd_trx2,0) as prd_trx2,
  coalesce(prd_trx3,0) as prd_trx3
 
from(
select * from have
 outer union corr
select * from cmpny_prod
)
group by mkt_id
order by mkt_id,prd_id
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 Feb 2016 02:31:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-the-data-using-PROC-SQL/m-p/248149#M46585</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-02-05T02:31:26Z</dc:date>
    </item>
    <item>
      <title>Re: Help with the data using  PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-the-data-using-PROC-SQL/m-p/248153#M46586</link>
      <description>&lt;P&gt;Thanks Ksharp forthe solution But if I have prd_id from cmpny_prod already exists in the have data then I do not want trx1-trx3 to be 0.&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; have&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt; hcp_id : &lt;SPAN class="token punctuation"&gt;$&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;3&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; mkt_id : &lt;SPAN class="token punctuation"&gt;$&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;2&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; prd_id :&lt;SPAN class="token punctuation"&gt;$&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;2&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; mk_trx1&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;mk_trx3 &lt;SPAN class="token number"&gt;2&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; prd_trx1&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;prd_trx3 &lt;SPAN class="token number"&gt;2&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token keyword"&gt;cards&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;SPAN class="token data string"&gt;
123 12 4 1 3 2 0 2 1
123 12 5 1 3 2 1 0 1&lt;BR /&gt;123 12 8 1 3 2 0 1 0
&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;BR /&gt;I want the ouput to be as follows(since prd_id = 8 already exists in have):&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output should be as follows:&lt;/P&gt;
&lt;P&gt;hcp_id mkt_id prd_id mkt_trx1 mkt_trx2 mkt_trx3 prd_trx1 prd_trx2 prd_trx3&lt;BR /&gt;123 12 4 1 3 2 0 2 1&lt;BR /&gt;123 12 5 1 3 2 1 0&amp;nbsp;1&lt;BR /&gt;123 12 8 1 3 2 0 1&amp;nbsp;0&lt;BR /&gt;123 12 9 1 3 2 0 0 0&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token punctuation"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Feb 2016 03:02:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-the-data-using-PROC-SQL/m-p/248153#M46586</guid>
      <dc:creator>pp2014</dc:creator>
      <dc:date>2016-02-05T03:02:58Z</dc:date>
    </item>
    <item>
      <title>Re: Help with the data using  PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-the-data-using-PROC-SQL/m-p/248154#M46587</link>
      <description>&lt;P&gt;I know there are many scenarios you need consider about . Your post is ambiguous .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input hcp_id : $3. mkt_id : $2. prd_id :$2. mk_trx1-mk_trx3 2. prd_trx1-prd_trx3 2.;
cards;
123 12 4 1 3 2 0 2 1
123 12 5 1 3 2 1 0 1
123 12 8 1 3 2 0 1 0
;
run;


data cmpny_prod;
input mkt_id :$2. prd_id : $2.;
cards;
12 8
12 9
;
run;
proc sql;
create table want as
 select
  max(hcp_id) as hcp_id,
  mkt_id,
  prd_id,
  max(mk_trx1) as mk_trx1,
  max(mk_trx2) as mk_trx2,
  max(mk_trx3) as mk_trx3,
  coalesce(prd_trx1,0) as prd_trx1,
  coalesce(prd_trx2,0) as prd_trx2,
  coalesce(prd_trx3,0) as prd_trx3
 
from(
select * from have
 outer union corr
select * from cmpny_prod where catx(' ',mkt_id,prd_id) not in ( select catx(' ',mkt_id,prd_id) from have)
)
group by mkt_id
order by mkt_id,prd_id
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 Feb 2016 03:32:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-the-data-using-PROC-SQL/m-p/248154#M46587</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-02-05T03:32:23Z</dc:date>
    </item>
    <item>
      <title>Re: Help with the data using  PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-the-data-using-PROC-SQL/m-p/248155#M46588</link>
      <description>&lt;P&gt;I would suggest&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input hcp_id :$3. mkt_id :$2. prd_id :$2. mkt_trx1-mkt_trx3 2. prd_trx1-prd_trx3 2.;
datalines;
123 12 4 1 3 2 0 2 1
123 12 5 1 3 2 1 1 1
;

data cmpny_prod;
input mkt_id :$2. prd_id :$2.;
datalines;
12 5
12 8
12 9
;

proc sql;
create table want as 
select b.hcp_id, b.mkt_id, a.prd_id, 
    b.mkt_trx1, b.mkt_trx2, b.mkt_trx3, 
    coalesce(c.prd_trx1, 0) as prd_trx1, 
    coalesce(c.prd_trx2, 0) as prd_trx2,
    coalesce(c.prd_trx3, 0) as prd_trx3
from 
    (   select mkt_id, prd_id from have 
        union 
        select mkt_id, prd_id from cmpny_prod ) as a inner join 
    (select unique hcp_id, mkt_id, mkt_trx1, mkt_trx2, mkt_trx3 from have) as b 
        on a.mkt_id=b.mkt_id left join 
    have as c 
        on b.hcp_id=c.hcp_id and b.mkt_id=c.mkt_id and a.prd_id=c.prd_id and 
            b.mkt_trx1=c.mkt_trx1 and b.mkt_trx2=c.mkt_trx2 and b.mkt_trx3=c.mkt_trx3;
select * from want;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;it looks messy, but it works.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Feb 2016 03:56:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-the-data-using-PROC-SQL/m-p/248155#M46588</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-02-05T03:56:42Z</dc:date>
    </item>
    <item>
      <title>Re: Help with the data using  PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-the-data-using-PROC-SQL/m-p/248156#M46589</link>
      <description>&lt;P&gt;Thanks Ksharp.. &amp;nbsp;Your solution works only if I have only one hcp_id. &amp;nbsp;If I have multiple hcp_ids like below, &amp;nbsp;it does not give me right solution...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;/P&gt;
&lt;P&gt;input hcp_id : $3. mkt_id : $2. prd_id :$2. mk_trx1-mk_trx3 2. prd_trx1-prd_trx3 2.;&lt;/P&gt;
&lt;P&gt;cards; 123 12 4 2 4 2 0 2 1&lt;/P&gt;
&lt;P&gt;123 12 5 2 4 2 1 0 1&lt;/P&gt;
&lt;P&gt;123 12 8 2 4 2 0 1 0&lt;/P&gt;
&lt;P&gt;123 12 9 2 4 2 1 1 0&lt;/P&gt;
&lt;P&gt;124 12 1 3 2 1 3 2 1&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data cmpny_prod;&lt;/P&gt;
&lt;P&gt;input mkt_id :$2. prd_id : $2.;&lt;/P&gt;
&lt;P&gt;cards;&lt;/P&gt;
&lt;P&gt;12 8&lt;/P&gt;
&lt;P&gt;12 9 ;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Feb 2016 03:58:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-the-data-using-PROC-SQL/m-p/248156#M46589</guid>
      <dc:creator>pp2014</dc:creator>
      <dc:date>2016-02-05T03:58:57Z</dc:date>
    </item>
    <item>
      <title>Re: Help with the data using  PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-the-data-using-PROC-SQL/m-p/248157#M46590</link>
      <description>&lt;P&gt;Thanks PG Stats..Your solution almost works except that I want ptrd_trx1-prd_trx3 to be 0 for the products in the cmpny_prod dataset only if they are not in have. &amp;nbsp;So If I have another hcp_id added to have, then according to your logic I am getting wrong results..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Feb 2016 04:09:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-the-data-using-PROC-SQL/m-p/248157#M46590</guid>
      <dc:creator>pp2014</dc:creator>
      <dc:date>2016-02-05T04:09:22Z</dc:date>
    </item>
    <item>
      <title>Re: Help with the data using  PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-the-data-using-PROC-SQL/m-p/248184#M46598</link>
      <description>&lt;P&gt;Ou.You are trying to do Cartesian Product ! I think it is better for data step .&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 have;
input hcp_id : $3. mkt_id : $2. prd_id :$2. mk_trx1-mk_trx3 2. prd_trx1-prd_trx3 2.;
cards; 123 12 4 2 4 2 0 2 1
123 12 5 2 4 2 1 0 1
123 12 8 2 4 2 0 1 0
123 12 9 2 4 2 1 1 0
124 12 1 3 2 1 3 2 1
;
run;
 
data cmpny_prod;
input mkt_id :$2. prd_id : $2.;
cards;
12 8
12 9 
;
run;

proc sql;
create table temp as
select *
 from (select distinct hcp_id from have),(select distinct mkt_id,prd_id from cmpny_prod);

create table want as
 select
  hcp_id,
  mkt_id,
  prd_id,
  max(mk_trx1) as mk_trx1,
  max(mk_trx2) as mk_trx2,
  max(mk_trx3) as mk_trx3,
  coalesce(prd_trx1,0) as prd_trx1,
  coalesce(prd_trx2,0) as prd_trx2,
  coalesce(prd_trx3,0) as prd_trx3
 
from(
select * from have
 outer union corr
select * from temp where catx(' ',hcp_id,mkt_id,prd_id) not in ( select catx(' ',hcp_id,mkt_id,prd_id) from have)
)
group by hcp_id,mkt_id
order by hcp_id,mkt_id,prd_id
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 Feb 2016 07:35:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-the-data-using-PROC-SQL/m-p/248184#M46598</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-02-05T07:35:18Z</dc:date>
    </item>
    <item>
      <title>Re: Help with the data using  PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-the-data-using-PROC-SQL/m-p/248523#M46690</link>
      <description>&lt;P&gt;last attempt:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input hcp_id :$3. mkt_id :$2. prd_id :$2. mkt_trx1-mkt_trx3 2. prd_trx1-prd_trx3 2.;
datalines;
123 12 4 1 3 2 0 2 1
123 12 5 1 3 2 1 1 1
124 12 1 3 2 1 3 2 1
;

data cmpny_prod;
input mkt_id :$2. prd_id :$2.;
datalines;
12 5
12 8
12 9
;

proc sql;
create table want as 
select b.hcp_id, b.mkt_id, a.prd_id, 
    b.mkt_trx1, b.mkt_trx2, b.mkt_trx3, 
    coalesce(c.prd_trx1, 0) as prd_trx1, 
    coalesce(c.prd_trx2, 0) as prd_trx2,
    coalesce(c.prd_trx3, 0) as prd_trx3
from 
    (   select hcp_id, mkt_id, prd_id from have 
        union 
        select hcp_id, mkt_id, prd_id from (select hcp_id from have), cmpny_prod ) as a inner join 
    (select unique hcp_id, mkt_id, mkt_trx1, mkt_trx2, mkt_trx3 from have) as b 
        on a.hcp_id=b.hcp_id and a.mkt_id=b.mkt_id left join 
    have as c 
        on b.hcp_id=c.hcp_id and b.mkt_id=c.mkt_id and a.prd_id=c.prd_id and 
            b.mkt_trx1=c.mkt_trx1 and b.mkt_trx2=c.mkt_trx2 and b.mkt_trx3=c.mkt_trx3;
select * from want;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 07 Feb 2016 02:31:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-the-data-using-PROC-SQL/m-p/248523#M46690</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-02-07T02:31:38Z</dc:date>
    </item>
  </channel>
</rss>

