<?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: Order of categories : cartesian product (cross join) of categorical variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Order-of-categories-cartesian-product-cross-join-of-categorical/m-p/583057#M165945</link>
    <description>&lt;P&gt;&lt;EM&gt;&amp;gt;Why is it not working in proc sql?&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Because &lt;EM&gt;cheep&lt;/EM&gt; does not have a value in the informat.&lt;/P&gt;</description>
    <pubDate>Thu, 22 Aug 2019 05:42:02 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2019-08-22T05:42:02Z</dc:date>
    <item>
      <title>Order of categories : cartesian product (cross join) of categorical variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Order-of-categories-cartesian-product-cross-join-of-categorical/m-p/583047#M165937</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;In the following example I produce a data set that includes all possible combinations of 3 categorical variables.&lt;/P&gt;
&lt;P&gt;The categorical values are defined in&amp;nbsp; 3 %let statements.&lt;/P&gt;
&lt;P&gt;Then I create 3 data sets (each data set contain the categories of one field)&lt;/P&gt;
&lt;P&gt;Then I am using cross join (Cartesian product ) to produce the output data set.&lt;/P&gt;
&lt;P&gt;In the output data set It is important for me to control the order of the categories.&lt;/P&gt;
&lt;P&gt;Required order :&lt;/P&gt;
&lt;P&gt;product A&amp;nbsp; &amp;nbsp;product B&amp;nbsp; &amp;nbsp;product C&lt;/P&gt;
&lt;P&gt;'free'&amp;nbsp; &amp;nbsp; 'cheap'&amp;nbsp; &amp;nbsp; 'normal'&amp;nbsp; &amp;nbsp; &amp;nbsp;'expensive'&lt;/P&gt;
&lt;P&gt;'low'&amp;nbsp; &amp;nbsp; &amp;nbsp;'medium'&amp;nbsp; &amp;nbsp; &amp;nbsp;'high'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My question is why the order is:&lt;/P&gt;
&lt;P&gt;Free &lt;BR /&gt;Normal&lt;BR /&gt;Expensive &lt;BR /&gt;Cheap&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(I defined that the order will be :&lt;/P&gt;
&lt;P&gt;Free&lt;BR /&gt;Cheap&lt;BR /&gt;Normal&lt;BR /&gt;Expensive&lt;/P&gt;
&lt;P&gt;)&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;%let COVa_colA = product A, product B, product C;
%let COVb_colB = free, cheep, normal, expensive;
%let COVc_colC = low, medium, high;


data temp1 (keep=cola)
     temp2 (keep=cola)
     temp3 (keep=cola);
  set sashelp.vmacro (where=(scope eq 'GLOBAL' and prxmatch('/^COV\w_.+$/i',name )));
  i=1;
  do while (scan(value,i) ne "");
    cola=scan(value,i);
    if name =: "COVA" then do;
      cola=catx(" ",cola,scan(value,i+1));
      output temp1;
      i+2;
    end;
    else if name =: "COVB" then do;
      output temp2;
      i+1;
    end;
    else if name =: "COVC" then do;
      output temp3;
      i+1;
    end;
  end;
run;



/*If we want to control the order of rows*/
proc format;
  invalue $orderb
    'free'=1
    'cheap'=2
    'normal'=3
    'expensive'=4;

  invalue $orderc
    'low'=1
    'medium'=2
    'high'=3;
run;


proc sql noprint;
  create table want  as
  select *
    from temp1,
         temp2 (rename=(cola=colb)),
         temp3 (rename=(cola=colc))
      order by cola,
               input(colb,$orderb.),
               input(colc,$orderc.);
  ;
quit;
/*
No of categories in Cat1 :3 (product A, product B, product C)
No of categories in Cat2 :4 (free, cheep, normal, expensive)
No of categories in Cat3 :3 (low, medium, high)
In output table we will get 36 obs beacause 3x4x3=36 
*/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Aug 2019 04:11:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Order-of-categories-cartesian-product-cross-join-of-categorical/m-p/583047#M165937</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-08-22T04:11:16Z</dc:date>
    </item>
    <item>
      <title>Re: Order of categories : cartesian product (cross join) of categorical variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Order-of-categories-cartesian-product-cross-join-of-categorical/m-p/583049#M165939</link>
      <description>&lt;P&gt;I found solution to control the order of categories but I still ask how to control it via proc sql?&lt;/P&gt;
&lt;P&gt;Why is it not working in proc sql?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want3;
 set temp1;
 do i=1 to _nobs;
  set temp2(rename=(cola=colb)) nobs=_nobs point=i;
   do j=1 to __nobs;
    set temp3(rename=(cola=colc)) nobs=__nobs point=j;
    output;
   end;
 end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 22 Aug 2019 04:17:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Order-of-categories-cartesian-product-cross-join-of-categorical/m-p/583049#M165939</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-08-22T04:17:39Z</dc:date>
    </item>
    <item>
      <title>Re: Order of categories : cartesian product (cross join) of categorical variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Order-of-categories-cartesian-product-cross-join-of-categorical/m-p/583057#M165945</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt;Why is it not working in proc sql?&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Because &lt;EM&gt;cheep&lt;/EM&gt; does not have a value in the informat.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Aug 2019 05:42:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Order-of-categories-cartesian-product-cross-join-of-categorical/m-p/583057#M165945</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-08-22T05:42:02Z</dc:date>
    </item>
  </channel>
</rss>

