<?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: proc sql: add one column from one table to another table and count in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-add-one-column-from-one-table-to-another-table-and/m-p/251474#M56706</link>
    <description>&lt;P&gt;Use a subset or generate sample data to test on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A good way to do this is to use OBS=10000 say to restrict your data to smaller sizes and test your results. &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;option obs=100000;

*run tests;



option obs=max;&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;The reality is these are business decisions to a point, the code may be correct but it may not answer your question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regarding your questions:&lt;/P&gt;
&lt;P&gt;Do you really want a left join or a full join? You want a left join if you want all ID's to be included even if they didn't order. You want a full join if you only want IDs&amp;nbsp;with orders.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have multiple orders per ID, then yes you will have a table larger than your original, and you can't depend on the counts to verify your merge. This is why it's important to generate some test cases and ensure you're getting what you expect.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code looks correct as far as I can see.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 22 Feb 2016 05:27:52 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-02-22T05:27:52Z</dc:date>
    <item>
      <title>proc sql: add one column from one table to another table and count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-add-one-column-from-one-table-to-another-table-and/m-p/251471#M56703</link>
      <description>&lt;P&gt;In table A, I have ID, gender, age, etc demographics info;&lt;/P&gt;
&lt;P&gt;In table B, I have ID, order_datetime, order_amount etc order details info.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table A has more IDs than table B (many IDs never put an order), and in table B each ID may have multiple orders.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I want to merge them, to have a new table, which has all info from Table A, and also order_datetime from Table B.&lt;/P&gt;
&lt;P&gt;In this case, shall I use the left join in proc sql?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table demo_order as
select a.ID, a.gender, a.age, b.order_date 
from demographics as a 
     left join 
     order_details as b
     on a.ID eq b.ID;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then, I will calculate how many orders one ID made per day. Can I use the following code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table order_per_day as
select ID, order_date,
       count(*) as num_orders
from demo_order
group by ID, order_date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also want to find out which IDs never put an order, and count how many. Can I count directly in the proc sql step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table never_ordered as
select ID, age, gender 
from demo_order
where order_date is missing;
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>Mon, 22 Feb 2016 05:15:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-add-one-column-from-one-table-to-another-table-and/m-p/251471#M56703</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-02-22T05:15:42Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql: add one column from one table to another table and count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-add-one-column-from-one-table-to-another-table-and/m-p/251472#M56704</link>
      <description>&lt;P&gt;Out of curiousity, why can't you test your code?&lt;/P&gt;</description>
      <pubDate>Mon, 22 Feb 2016 05:13:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-add-one-column-from-one-table-to-another-table-and/m-p/251472#M56704</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-02-22T05:13:20Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql: add one column from one table to another table and count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-add-one-column-from-one-table-to-another-table-and/m-p/251473#M56705</link>
      <description>&lt;P&gt;data set is very large. I ran the first step, and there was no error.The new table has many many more rows than the original table A. I guess this is due to some IDs have multiple orders. &lt;/P&gt;
&lt;P&gt;I want to make sure I did it correctly before moving forward.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Feb 2016 05:19:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-add-one-column-from-one-table-to-another-table-and/m-p/251473#M56705</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-02-22T05:19:48Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql: add one column from one table to another table and count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-add-one-column-from-one-table-to-another-table-and/m-p/251474#M56706</link>
      <description>&lt;P&gt;Use a subset or generate sample data to test on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A good way to do this is to use OBS=10000 say to restrict your data to smaller sizes and test your results. &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;option obs=100000;

*run tests;



option obs=max;&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;The reality is these are business decisions to a point, the code may be correct but it may not answer your question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regarding your questions:&lt;/P&gt;
&lt;P&gt;Do you really want a left join or a full join? You want a left join if you want all ID's to be included even if they didn't order. You want a full join if you only want IDs&amp;nbsp;with orders.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have multiple orders per ID, then yes you will have a table larger than your original, and you can't depend on the counts to verify your merge. This is why it's important to generate some test cases and ensure you're getting what you expect.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code looks correct as far as I can see.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Feb 2016 05:27:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-add-one-column-from-one-table-to-another-table-and/m-p/251474#M56706</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-02-22T05:27:52Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql: add one column from one table to another table and count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-add-one-column-from-one-table-to-another-table-and/m-p/251475#M56707</link>
      <description>Thank you very much for your prompt replies, and your suggestions. Yes, it is a good idea to use a small portion of the data set to test. &lt;BR /&gt;&lt;BR /&gt;when you said: "You want a full join if you only want IDs with orders." -- it should be inner join, not full join, right?</description>
      <pubDate>Mon, 22 Feb 2016 05:41:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-add-one-column-from-one-table-to-another-table-and/m-p/251475#M56707</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-02-22T05:41:34Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql: add one column from one table to another table and count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-add-one-column-from-one-table-to-another-table-and/m-p/251501#M56711</link>
      <description>&lt;P&gt;Can you post some example test data - in the form of a datastep - and what you want the output to look like. &amp;nbsp;I can't really provide any code here without something to work with, however I will say the following:&lt;/P&gt;
&lt;P&gt;Why do you join a large dataset, to another large dataset, and then from that start summarizing the data? &amp;nbsp;This is most process intensive. &amp;nbsp;Why do you not summarize the order dataset first, and then merge the summarized data onto the other dataset? &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, have you tried doing this directly in one datastep? &amp;nbsp;Perfomance may be better by doing it that way, and from the logic I don't see anything too difficult. &amp;nbsp;Provide some test data and can have a look.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Feb 2016 09:39:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-add-one-column-from-one-table-to-another-table-and/m-p/251501#M56711</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-02-22T09:39:34Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql: add one column from one table to another table and count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-add-one-column-from-one-table-to-another-table-and/m-p/251550#M56717</link>
      <description>Thank you, RW9. You are right. It would be more efficient to count the order before merge. &lt;BR /&gt;&lt;BR /&gt;I also dropped many variables when I merge first, which was to reduce the processing time too.</description>
      <pubDate>Mon, 22 Feb 2016 14:51:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-add-one-column-from-one-table-to-another-table-and/m-p/251550#M56717</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-02-22T14:51:49Z</dc:date>
    </item>
  </channel>
</rss>

