<?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: cartesian product in  SAS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896829#M354379</link>
    <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 02 Oct 2023 21:50:17 GMT</pubDate>
    <dc:creator>MarkusWeick</dc:creator>
    <dc:date>2023-10-02T21:50:17Z</dc:date>
    <item>
      <title>cartesian product in  SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896787#M354359</link>
      <description>&lt;P&gt;I am trying to get all combinations ( Cartesian product) of two datasets in sas using a unique ID.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* Create Data A */&lt;BR /&gt;data a;&lt;BR /&gt;input id gl1;&lt;BR /&gt;datalines;&lt;BR /&gt;1 100&lt;BR /&gt;1 300&lt;BR /&gt;2 400&lt;BR /&gt;3 100&lt;BR /&gt;3 400&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;/* Create Data B */&lt;BR /&gt;data b;&lt;BR /&gt;input id gl2;&lt;BR /&gt;datalines;&lt;BR /&gt;1 300&lt;BR /&gt;1 400&lt;BR /&gt;2 100&lt;BR /&gt;2 200&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want the output to look like this:&lt;/P&gt;&lt;P&gt;id gl1 gl2&lt;/P&gt;&lt;P&gt;1 100 300&lt;/P&gt;&lt;P&gt;1 100 400&lt;/P&gt;&lt;P&gt;1 300 300&lt;/P&gt;&lt;P&gt;1 300 400&lt;/P&gt;&lt;P&gt;2 400 100&lt;/P&gt;&lt;P&gt;2 400 200&lt;/P&gt;&lt;P&gt;3 100&amp;nbsp;&amp;nbsp; .&lt;/P&gt;&lt;P&gt;4&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 400&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone please help?&lt;/P&gt;&lt;P&gt;This code below only allows ids that are both in dataset a and b,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;proc sql;&lt;BR /&gt;create table c as&lt;BR /&gt;select *&lt;BR /&gt;from a, b&lt;BR /&gt;where a.ID=b.ID or ;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 19:54:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896787#M354359</guid>
      <dc:creator>nehachawla</dc:creator>
      <dc:date>2023-10-02T19:54:20Z</dc:date>
    </item>
    <item>
      <title>Re: cartesian product in  SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896800#M354362</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/455845"&gt;@nehachawla&lt;/a&gt;, if you use a full join like&lt;/P&gt;
&lt;PRE&gt;select coalesce(a.&lt;SPAN&gt;id, b.id) gl1 gl2&lt;/SPAN&gt;
    from a full join b
    on a.ID = b.ID;
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;you should also get the the IDs which are only in one of the data sets.&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 20:18:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896800#M354362</guid>
      <dc:creator>MarkusWeick</dc:creator>
      <dc:date>2023-10-02T20:18:56Z</dc:date>
    </item>
    <item>
      <title>Re: cartesian product in  SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896803#M354364</link>
      <description>&lt;P&gt;Thank you for your prompt response, I have never used this coalese function and I get this error. can you assist?&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;1039 proc sql;&lt;BR /&gt;1040 create table c as&lt;BR /&gt;1041&lt;BR /&gt;1042 select coalesce(a.id, b.id) gl1 gl2&lt;BR /&gt;---&lt;BR /&gt;22&lt;BR /&gt;202&lt;BR /&gt;ERROR 22-322: Syntax error, expecting one of the following: a quoted string, !, !!, &amp;amp;, *, **,&lt;BR /&gt;+, ',', -, /, &amp;lt;, &amp;lt;=, &amp;lt;&amp;gt;, =, &amp;gt;, &amp;gt;=, ?, AND, AS, BETWEEN, CONTAINS, EQ, EQT,&lt;BR /&gt;FORMAT, FROM, GE, GET, GT, GTT, IN, INFORMAT, INTO, IS, LABEL, LE, LEN, LENGTH,&lt;BR /&gt;LET, LIKE, LT, LTT, NE, NET, NOT, NOTIN, OR, TRANSCODE, ^, ^=, |, ||, ~, ~=.&lt;/P&gt;&lt;P&gt;ERROR 202-322: The option or parameter is not recognized and will be ignored.&lt;/P&gt;&lt;P&gt;1043 from a full join b&lt;BR /&gt;1044 on a.ID = b.ID;&lt;BR /&gt;1045 quit;&lt;BR /&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;real time 0.00 seconds&lt;BR /&gt;cpu time 0.00 seconds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 20:30:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896803#M354364</guid>
      <dc:creator>nehachawla</dc:creator>
      <dc:date>2023-10-02T20:30:10Z</dc:date>
    </item>
    <item>
      <title>Re: cartesian product in  SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896808#M354366</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/455845"&gt;@nehachawla&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;I think I was to quick.&lt;/P&gt;
&lt;P&gt;please try&amp;nbsp;&lt;/P&gt;
&lt;P&gt;select&amp;nbsp;&lt;SPAN&gt;coalesce(a.id, b.id) as id format = 10, gl1, gl2&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;instead of&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;select coalesce(a.id, b.id) gl1 gl2&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 20:36:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896808#M354366</guid>
      <dc:creator>MarkusWeick</dc:creator>
      <dc:date>2023-10-02T20:36:22Z</dc:date>
    </item>
    <item>
      <title>Re: cartesian product in  SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896820#M354370</link>
      <description>&lt;P&gt;Mike using this , I still get this error- see attached.&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 21:12:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896820#M354370</guid>
      <dc:creator>nehachawla</dc:creator>
      <dc:date>2023-10-02T21:12:55Z</dc:date>
    </item>
    <item>
      <title>Re: cartesian product in  SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896821#M354371</link>
      <description />
      <pubDate>Mon, 02 Oct 2023 21:13:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896821#M354371</guid>
      <dc:creator>nehachawla</dc:creator>
      <dc:date>2023-10-02T21:13:25Z</dc:date>
    </item>
    <item>
      <title>Re: cartesian product in  SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896822#M354372</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/455845"&gt;@nehachawla&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;could you please post the error into the chat, as I can't open attachments.&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 21:31:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896822#M354372</guid>
      <dc:creator>MarkusWeick</dc:creator>
      <dc:date>2023-10-02T21:31:12Z</dc:date>
    </item>
    <item>
      <title>Re: cartesian product in  SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896823#M354373</link>
      <description>&lt;P&gt;157 proc sql;&lt;BR /&gt;1158 create table c as&lt;BR /&gt;1159 select coalesce(a.id, b.id) as id format = &lt;STRONG&gt;&lt;U&gt;10&lt;/U&gt;&lt;/STRONG&gt;, gl1, gl2&lt;BR /&gt;--&lt;BR /&gt;22&lt;BR /&gt;76&lt;BR /&gt;ERROR 22-322: Expecting a format name.&lt;/P&gt;&lt;P&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;&lt;P&gt;1160 from a full join b&lt;BR /&gt;1161 on a.ID = b.ID;&lt;BR /&gt;1162 quit;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 21:39:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896823#M354373</guid>
      <dc:creator>nehachawla</dc:creator>
      <dc:date>2023-10-02T21:39:41Z</dc:date>
    </item>
    <item>
      <title>Re: cartesian product in  SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896826#M354376</link>
      <description>&lt;P&gt;You have to list the variables you want in the SELECT clause separated by COMMAS (welcome to working with SQL syntax &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; ).&amp;nbsp; You also need to give this new variable a NAME.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table c as
  select coalesce(a.id, b.id) as id
       , a.gl1 
       , b.gl2
  from a full join b
    on a.ID = b.ID
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    id    gl1    gl2

 1      1    100    300
 2      1    100    400
 3      1    300    300
 4      1    300    400
 5      2    400    100
 6      2    400    200
 7      3    400      .
 8      3    100      .
&lt;/PRE&gt;
&lt;P&gt;Note if you know that ID is the only variable in common between the two datasets you can just a NATURAL join.&amp;nbsp; SAS will then generate the COALESCE() and ON for you so the syntax is a lot simpler.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table c as
  select *
  from b natural full join a
;
quit;&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>Mon, 02 Oct 2023 21:46:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896826#M354376</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-10-02T21:46:49Z</dc:date>
    </item>
    <item>
      <title>Re: cartesian product in  SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896829#M354379</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 21:50:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896829#M354379</guid>
      <dc:creator>MarkusWeick</dc:creator>
      <dc:date>2023-10-02T21:50:17Z</dc:date>
    </item>
    <item>
      <title>Re: cartesian product in  SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896830#M354380</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/455845"&gt;@nehachawla&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;I missed the . in the format:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;select coalesce(a.id, b.id) as id format =&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;&lt;U&gt;10&lt;/U&gt;&lt;/STRONG&gt;&lt;SPAN&gt;. gl1, gl2&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 21:53:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896830#M354380</guid>
      <dc:creator>MarkusWeick</dc:creator>
      <dc:date>2023-10-02T21:53:08Z</dc:date>
    </item>
    <item>
      <title>Re: cartesian product in  SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896835#M354383</link>
      <description>&lt;P&gt;Thank you Tom- This worked like a charm&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 22:42:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896835#M354383</guid>
      <dc:creator>nehachawla</dc:creator>
      <dc:date>2023-10-02T22:42:09Z</dc:date>
    </item>
    <item>
      <title>Re: cartesian product in  SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896836#M354384</link>
      <description>&lt;P&gt;Thanks Markus for the lead&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 22:42:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/cartesian-product-in-SAS/m-p/896836#M354384</guid>
      <dc:creator>nehachawla</dc:creator>
      <dc:date>2023-10-02T22:42:42Z</dc:date>
    </item>
  </channel>
</rss>

