<?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: SPARSE option in proc freq. Is it possible to only include all possible values of one variable? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847429#M335035</link>
    <description>&lt;P&gt;You're welcome.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first idea was to create a table combining all distinct treatments in dataset HAVE with all&amp;nbsp;distinct (&lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;) pairs&amp;nbsp;in dataset HAVE. This is a Cartesian product join of the form&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select * from
  (select distinct trt from have),
  (select distinct color, brand from have);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But the same result can be obtained from the shorter (but probably less efficient) query&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select distinct * from
  have(keep=trt), have(drop=trt);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which combines&amp;nbsp;all treatments (including duplicates) in dataset HAVE with all (&lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;) pairs&amp;nbsp;(again including duplicates) in dataset HAVE, eventually removing duplicates from this set of combinations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used the latter query as an inline view in the outer query, which has the form of a natural right join, i.e., a join using all like-named columns of the contributing tables as key variables: &lt;FONT face="courier new,courier"&gt;trt&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;. The &lt;EM&gt;right&lt;/EM&gt; join ensures that the output dataset WANT will contain all (&lt;FONT face="courier new,courier"&gt;trt&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;) combinations of the inline view constructed above and, most importantly, it helps us to detect those combinations which are not contained in dataset HAVE (the left part of the join). This is necessary to get the zeros into the final table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A&amp;nbsp;(&lt;FONT face="courier new,courier"&gt;trt&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;) combination which is not contained in HAVE contributes missing values for all three variables to the join. These are normally invisible, though, because&amp;nbsp;we only see the non-missing values contributed by the inline view. But the explicit reference&amp;nbsp;&lt;FONT face="courier new,courier"&gt;&lt;EM&gt;a&lt;/EM&gt;.trt&lt;/FONT&gt; (and similarly &lt;FONT face="courier new,courier"&gt;&lt;EM&gt;a&lt;/EM&gt;.color&lt;/FONT&gt; or &lt;FONT face="courier new,courier"&gt;&lt;EM&gt;a&lt;/EM&gt;.brand&lt;/FONT&gt;) allows us to work with the values contributed by HAVE (with alias &lt;FONT face="courier new,courier"&gt;a&lt;/FONT&gt;). The number of non-missing &lt;FONT face="courier new,courier"&gt;a.trt&lt;/FONT&gt; values per (&lt;FONT face="courier new,courier"&gt;trt&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;) combination is what we need in variable &lt;FONT face="courier new,courier"&gt;count&lt;/FONT&gt; of dataset WANT. So we compute &lt;FONT face="courier new,courier"&gt;count(a.trt)&lt;/FONT&gt; for each combination.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The variables created by the natural join are the same as in dataset HAVE, in the same order: &lt;FONT face="courier new,courier"&gt;trt&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;. We can refer to them by their positions 1, 2, 3, respectively, in the GROUP BY clause. Your desired output suggests a sort order by &lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;trt&lt;/FONT&gt;, which is obtained by &lt;FONT face="courier new,courier"&gt;group by 2, 3, 1&lt;/FONT&gt;&amp;nbsp;(surprisingly not by&amp;nbsp;&lt;FONT face="courier new,courier"&gt;group by color, brand, trt&lt;/FONT&gt;). To ensure the desired sort order of dataset WANT for your real input data you should insert an ORDER BY clause after the GROUP BY clause:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;order by 2, 3, 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Edit / Addendum:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;I was "surprised" that &lt;FONT face="courier new,courier"&gt;group by 2, 3, 1&lt;/FONT&gt; produced a different sort order of dataset WANT than &lt;FONT face="courier new,courier"&gt;group by color, brand, trt&lt;/FONT&gt;, but instead I should have been surprised that the GROUP BY clause using the explicit variable names worked at all! In an ordinary (i.e., not &lt;FONT face="courier new,courier"&gt;natural&lt;/FONT&gt;) right join a GROUP BY clause like this would have caused the PROC SQL step to fail with error messages&lt;/P&gt;
&lt;PRE&gt;ERROR: Ambiguous reference, column color is in more than one table.
ERROR: Ambiguous reference, column brand is in more than one table.
ERROR: Ambiguous reference, column trt is in more than one table.&lt;/PRE&gt;
&lt;P&gt;because both dataset HAVE (with alias &lt;FONT face="courier new,courier"&gt;a&lt;/FONT&gt;) and the inline view contribute these same variable names to the join. Even if the errors were avoided by being more specific in the variable references in the GROUP BY clause, the "&lt;FONT face="courier new,courier"&gt;select *&lt;/FONT&gt;" of the outer query would at least cause warning messages&lt;/P&gt;
&lt;PRE&gt;WARNING: Variable trt already exists on file WORK.WANT.
WARNING: Variable color already exists on file WORK.WANT.
WARNING: Variable brand already exists on file WORK.WANT.&lt;/PRE&gt;
&lt;P&gt;reminding us of those name conflicts. Yet, all this doesn't happen and this appears to be a special (convenience) feature of &lt;FONT face="courier new,courier"&gt;natural&lt;/FONT&gt; joins.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/sqlproc/p12ohgh32ffm6un13s7l2d5p9c8y.htm#n0dl95cpzwue89n1iobwxfguflpz" target="_blank" rel="noopener"&gt;FEEDBACK option&lt;/A&gt; of the PROC SQL statement reveals what happens inside the natural join:&lt;/P&gt;
&lt;PRE&gt;1141  proc sql &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;feedback&lt;/STRONG&gt;&lt;/FONT&gt;;
1142  create table want as
1143  select *, count(a.trt) as count from
1144  have a
1145  natural right join
1146  (select distinct * from have(keep=trt) &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;b&lt;/STRONG&gt;&lt;/FONT&gt;, have(drop=trt) &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;c&lt;/STRONG&gt;&lt;/FONT&gt;)
1147  group by color, brand, trt;
NOTE: Statement transforms to:

        select COALESCE(A.trt, B.trt) as trt, COALESCE(A.color, C.color) as color, COALESCE(A.brand, C.brand) as brand, COUNT(A.trt) as count
          from WORK.HAVE A right outer join
               ( select distinct B.trt, C.color, C.brand
                   from WORK.HAVE B(keep=trt), WORK.HAVE C(drop=trt)
               ) on (A.trt = B.trt) and (A.color = C.color) and (A.brand = C.brand)
      group by C.color, C.brand, B.trt;

NOTE: The execution of this query involves performing one or more Cartesian product joins that can not be optimized.
NOTE: The query requires remerging summary statistics back with the original data.
NOTE: Table WORK.WANT created, with 12 rows and 4 columns.&lt;/PRE&gt;
&lt;P&gt;(For the above log I had also added aliases &lt;FONT face="courier new,courier"&gt;b&lt;/FONT&gt;&amp;nbsp;and &lt;FONT face="courier new,courier"&gt;c&lt;/FONT&gt; to the source datasets of the inline view in order to clarify the code provided by the&amp;nbsp;FEEDBACK option.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So it turns out that with the natural join the compiler decided to (tacitly) use&amp;nbsp;&lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;&amp;nbsp;and&amp;nbsp;&lt;FONT face="courier new,courier"&gt;trt&lt;/FONT&gt; &lt;EM&gt;from the inline view&lt;/EM&gt;, thus avoiding the harmful ambiguities. The resolution of the asterisk in the "&lt;FONT face="courier new,courier"&gt;select *&lt;/FONT&gt;" of the outer query to COALESCE expressions as shown above is standard behavior of natural joins and avoids the warnings mentioned earlier.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The numbers 1, 2, 3 used in "&lt;FONT face="courier new,courier"&gt;group by 2, 3, 1&lt;/FONT&gt;", however, always refer to the items in the (outer) SELECT clause. In our case:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;FONT face="courier new,courier"&gt;COALESCE(A.trt, B.trt)&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="courier new,courier"&gt;COALESCE(A.color, C.color)&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="courier new,courier"&gt;COALESCE(A.brand, C.brand)&lt;/FONT&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;While the natural join assigned the original variable names &lt;FONT face="courier new,courier"&gt;trt&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;&amp;nbsp;to these three items, they are, of course, different entities than &lt;FONT face="courier new,courier"&gt;C.color&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;C.brand&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;B.trt&lt;/FONT&gt;. So,&amp;nbsp;in this case "&lt;FONT face="courier new,courier"&gt;group by 2, 3, 1&lt;/FONT&gt;" is &lt;EM&gt;not&lt;/EM&gt; an abbreviation of "&lt;FONT face="courier new,courier"&gt;group by color, brand, trt&lt;/FONT&gt;" (in the sense of "&lt;FONT face="courier new,courier"&gt;group by C.color, C.brand, B.trt&lt;/FONT&gt;"), but rather of&lt;/P&gt;
&lt;PRE&gt;group by &lt;EM&gt;calculated&lt;/EM&gt; color, &lt;EM&gt;calculated&lt;/EM&gt; brand, &lt;EM&gt;calculated&lt;/EM&gt; trt&lt;/PRE&gt;
&lt;P&gt;which produces the same sort order of dataset WANT.&lt;/P&gt;</description>
    <pubDate>Fri, 02 Dec 2022 21:18:23 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2022-12-02T21:18:23Z</dc:date>
    <item>
      <title>SPARSE option in proc freq. Is it possible to only include all possible values of one variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847266#M334990</link>
      <description>&lt;P&gt;I have a table where I'm trying to populate 0's for all treatments. There are 3 possible treatments. I don't need all possible combinations of values for all variables, but I need all possible values of treatment. Can the SPARSE function handle this with an option I'm unfamiliar with? Is there a better way?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dsd dlm=",";
	input trt $ color $ brand $;
datalines;
1, blue, chevy
2, blue, chevy
3, red, ford
2, yellow, chevy
1, red, toyota
;
run;

proc freq data=have noprint;
	table trt*color*brand/ sparse list nopercent nocum norow nocol out=count (drop=percent);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;desired output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Hello_there_1-1669909157258.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77905i7F16E463BEF3CA8E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Hello_there_1-1669909157258.png" alt="Hello_there_1-1669909157258.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 15:40:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847266#M334990</guid>
      <dc:creator>Hello_there</dc:creator>
      <dc:date>2022-12-01T15:40:54Z</dc:date>
    </item>
    <item>
      <title>Re: SPARSE option in proc freq. Is it possible to only include all possible values of one variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847318#M335006</link>
      <description>&lt;P&gt;I don't think this can be done directly in Proc Freq. Here is an approach in the Data Step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop = t);

   if _N_ = 1 then do;
      dcl hash h(dataset : 'have');
      h.definekey(all : 'Y');
      h.definedone();
   end;

   set have;

   do t = 1 to 3;
      trt = put(t, 8.);
      if h.check() then do;
         output;
         h.add();
      end;
   end;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 01 Dec 2022 19:21:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847318#M335006</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-12-01T19:21:58Z</dc:date>
    </item>
    <item>
      <title>Re: SPARSE option in proc freq. Is it possible to only include all possible values of one variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847325#M335010</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/252358"&gt;@Hello_there&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think you would need to apply SPARSE to a crosstabulation of&amp;nbsp;&lt;FONT face="courier new,courier"&gt;comb*trt&lt;/FONT&gt; where &lt;FONT face="courier new,courier"&gt;comb&lt;/FONT&gt; is a concatenation of &lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;, created in a preliminary step (and eventually separated again in a post-processing step). But then it would be easier to get everything in one step as shown by &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304" target="_blank" rel="noopener"&gt;PeterClemmensen&lt;/A&gt; or in the PROC SQL step below.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select *, count(a.trt) as count from 
have a
natural right join
(select distinct * from have(keep=trt), have(drop=trt))
group by 2,3,1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(This assumes that all treatments occur in dataset HAVE.)&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 19:59:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847325#M335010</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-12-01T19:59:55Z</dc:date>
    </item>
    <item>
      <title>Re: SPARSE option in proc freq. Is it possible to only include all possible values of one variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847376#M335017</link>
      <description>Thanks for the reply!</description>
      <pubDate>Fri, 02 Dec 2022 00:26:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847376#M335017</guid>
      <dc:creator>Hello_there</dc:creator>
      <dc:date>2022-12-02T00:26:44Z</dc:date>
    </item>
    <item>
      <title>Re: SPARSE option in proc freq. Is it possible to only include all possible values of one variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847377#M335018</link>
      <description>Hi FreelanceReinhard,&lt;BR /&gt;&lt;BR /&gt;I am familiar with PROC SQL, but this is kind of a more sophisticated query than I'm used to. If you don't mind would you be able to explain what is happening? Also, i know that 2, 3, 1 refers to the position of the selected variables, but I don't which specifically those are referencing. &lt;BR /&gt;&lt;BR /&gt;Thanks for your reply, this is helping me learn</description>
      <pubDate>Fri, 02 Dec 2022 00:31:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847377#M335018</guid>
      <dc:creator>Hello_there</dc:creator>
      <dc:date>2022-12-02T00:31:04Z</dc:date>
    </item>
    <item>
      <title>Re: SPARSE option in proc freq. Is it possible to only include all possible values of one variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847429#M335035</link>
      <description>&lt;P&gt;You're welcome.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first idea was to create a table combining all distinct treatments in dataset HAVE with all&amp;nbsp;distinct (&lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;) pairs&amp;nbsp;in dataset HAVE. This is a Cartesian product join of the form&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select * from
  (select distinct trt from have),
  (select distinct color, brand from have);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But the same result can be obtained from the shorter (but probably less efficient) query&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select distinct * from
  have(keep=trt), have(drop=trt);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which combines&amp;nbsp;all treatments (including duplicates) in dataset HAVE with all (&lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;) pairs&amp;nbsp;(again including duplicates) in dataset HAVE, eventually removing duplicates from this set of combinations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used the latter query as an inline view in the outer query, which has the form of a natural right join, i.e., a join using all like-named columns of the contributing tables as key variables: &lt;FONT face="courier new,courier"&gt;trt&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;. The &lt;EM&gt;right&lt;/EM&gt; join ensures that the output dataset WANT will contain all (&lt;FONT face="courier new,courier"&gt;trt&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;) combinations of the inline view constructed above and, most importantly, it helps us to detect those combinations which are not contained in dataset HAVE (the left part of the join). This is necessary to get the zeros into the final table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A&amp;nbsp;(&lt;FONT face="courier new,courier"&gt;trt&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;) combination which is not contained in HAVE contributes missing values for all three variables to the join. These are normally invisible, though, because&amp;nbsp;we only see the non-missing values contributed by the inline view. But the explicit reference&amp;nbsp;&lt;FONT face="courier new,courier"&gt;&lt;EM&gt;a&lt;/EM&gt;.trt&lt;/FONT&gt; (and similarly &lt;FONT face="courier new,courier"&gt;&lt;EM&gt;a&lt;/EM&gt;.color&lt;/FONT&gt; or &lt;FONT face="courier new,courier"&gt;&lt;EM&gt;a&lt;/EM&gt;.brand&lt;/FONT&gt;) allows us to work with the values contributed by HAVE (with alias &lt;FONT face="courier new,courier"&gt;a&lt;/FONT&gt;). The number of non-missing &lt;FONT face="courier new,courier"&gt;a.trt&lt;/FONT&gt; values per (&lt;FONT face="courier new,courier"&gt;trt&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;) combination is what we need in variable &lt;FONT face="courier new,courier"&gt;count&lt;/FONT&gt; of dataset WANT. So we compute &lt;FONT face="courier new,courier"&gt;count(a.trt)&lt;/FONT&gt; for each combination.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The variables created by the natural join are the same as in dataset HAVE, in the same order: &lt;FONT face="courier new,courier"&gt;trt&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;. We can refer to them by their positions 1, 2, 3, respectively, in the GROUP BY clause. Your desired output suggests a sort order by &lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;trt&lt;/FONT&gt;, which is obtained by &lt;FONT face="courier new,courier"&gt;group by 2, 3, 1&lt;/FONT&gt;&amp;nbsp;(surprisingly not by&amp;nbsp;&lt;FONT face="courier new,courier"&gt;group by color, brand, trt&lt;/FONT&gt;). To ensure the desired sort order of dataset WANT for your real input data you should insert an ORDER BY clause after the GROUP BY clause:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;order by 2, 3, 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Edit / Addendum:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;I was "surprised" that &lt;FONT face="courier new,courier"&gt;group by 2, 3, 1&lt;/FONT&gt; produced a different sort order of dataset WANT than &lt;FONT face="courier new,courier"&gt;group by color, brand, trt&lt;/FONT&gt;, but instead I should have been surprised that the GROUP BY clause using the explicit variable names worked at all! In an ordinary (i.e., not &lt;FONT face="courier new,courier"&gt;natural&lt;/FONT&gt;) right join a GROUP BY clause like this would have caused the PROC SQL step to fail with error messages&lt;/P&gt;
&lt;PRE&gt;ERROR: Ambiguous reference, column color is in more than one table.
ERROR: Ambiguous reference, column brand is in more than one table.
ERROR: Ambiguous reference, column trt is in more than one table.&lt;/PRE&gt;
&lt;P&gt;because both dataset HAVE (with alias &lt;FONT face="courier new,courier"&gt;a&lt;/FONT&gt;) and the inline view contribute these same variable names to the join. Even if the errors were avoided by being more specific in the variable references in the GROUP BY clause, the "&lt;FONT face="courier new,courier"&gt;select *&lt;/FONT&gt;" of the outer query would at least cause warning messages&lt;/P&gt;
&lt;PRE&gt;WARNING: Variable trt already exists on file WORK.WANT.
WARNING: Variable color already exists on file WORK.WANT.
WARNING: Variable brand already exists on file WORK.WANT.&lt;/PRE&gt;
&lt;P&gt;reminding us of those name conflicts. Yet, all this doesn't happen and this appears to be a special (convenience) feature of &lt;FONT face="courier new,courier"&gt;natural&lt;/FONT&gt; joins.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/sqlproc/p12ohgh32ffm6un13s7l2d5p9c8y.htm#n0dl95cpzwue89n1iobwxfguflpz" target="_blank" rel="noopener"&gt;FEEDBACK option&lt;/A&gt; of the PROC SQL statement reveals what happens inside the natural join:&lt;/P&gt;
&lt;PRE&gt;1141  proc sql &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;feedback&lt;/STRONG&gt;&lt;/FONT&gt;;
1142  create table want as
1143  select *, count(a.trt) as count from
1144  have a
1145  natural right join
1146  (select distinct * from have(keep=trt) &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;b&lt;/STRONG&gt;&lt;/FONT&gt;, have(drop=trt) &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;c&lt;/STRONG&gt;&lt;/FONT&gt;)
1147  group by color, brand, trt;
NOTE: Statement transforms to:

        select COALESCE(A.trt, B.trt) as trt, COALESCE(A.color, C.color) as color, COALESCE(A.brand, C.brand) as brand, COUNT(A.trt) as count
          from WORK.HAVE A right outer join
               ( select distinct B.trt, C.color, C.brand
                   from WORK.HAVE B(keep=trt), WORK.HAVE C(drop=trt)
               ) on (A.trt = B.trt) and (A.color = C.color) and (A.brand = C.brand)
      group by C.color, C.brand, B.trt;

NOTE: The execution of this query involves performing one or more Cartesian product joins that can not be optimized.
NOTE: The query requires remerging summary statistics back with the original data.
NOTE: Table WORK.WANT created, with 12 rows and 4 columns.&lt;/PRE&gt;
&lt;P&gt;(For the above log I had also added aliases &lt;FONT face="courier new,courier"&gt;b&lt;/FONT&gt;&amp;nbsp;and &lt;FONT face="courier new,courier"&gt;c&lt;/FONT&gt; to the source datasets of the inline view in order to clarify the code provided by the&amp;nbsp;FEEDBACK option.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So it turns out that with the natural join the compiler decided to (tacitly) use&amp;nbsp;&lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;&amp;nbsp;and&amp;nbsp;&lt;FONT face="courier new,courier"&gt;trt&lt;/FONT&gt; &lt;EM&gt;from the inline view&lt;/EM&gt;, thus avoiding the harmful ambiguities. The resolution of the asterisk in the "&lt;FONT face="courier new,courier"&gt;select *&lt;/FONT&gt;" of the outer query to COALESCE expressions as shown above is standard behavior of natural joins and avoids the warnings mentioned earlier.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The numbers 1, 2, 3 used in "&lt;FONT face="courier new,courier"&gt;group by 2, 3, 1&lt;/FONT&gt;", however, always refer to the items in the (outer) SELECT clause. In our case:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;FONT face="courier new,courier"&gt;COALESCE(A.trt, B.trt)&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="courier new,courier"&gt;COALESCE(A.color, C.color)&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="courier new,courier"&gt;COALESCE(A.brand, C.brand)&lt;/FONT&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;While the natural join assigned the original variable names &lt;FONT face="courier new,courier"&gt;trt&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;color&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;brand&lt;/FONT&gt;&amp;nbsp;to these three items, they are, of course, different entities than &lt;FONT face="courier new,courier"&gt;C.color&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;C.brand&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;B.trt&lt;/FONT&gt;. So,&amp;nbsp;in this case "&lt;FONT face="courier new,courier"&gt;group by 2, 3, 1&lt;/FONT&gt;" is &lt;EM&gt;not&lt;/EM&gt; an abbreviation of "&lt;FONT face="courier new,courier"&gt;group by color, brand, trt&lt;/FONT&gt;" (in the sense of "&lt;FONT face="courier new,courier"&gt;group by C.color, C.brand, B.trt&lt;/FONT&gt;"), but rather of&lt;/P&gt;
&lt;PRE&gt;group by &lt;EM&gt;calculated&lt;/EM&gt; color, &lt;EM&gt;calculated&lt;/EM&gt; brand, &lt;EM&gt;calculated&lt;/EM&gt; trt&lt;/PRE&gt;
&lt;P&gt;which produces the same sort order of dataset WANT.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Dec 2022 21:18:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847429#M335035</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-12-02T21:18:23Z</dc:date>
    </item>
    <item>
      <title>Re: SPARSE option in proc freq. Is it possible to only include all possible values of one variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847448#M335043</link>
      <description>Thanks for taking time to type this out and teach me, FreelanceReinhard! I will be studying this thoroughly.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 02 Dec 2022 14:25:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847448#M335043</guid>
      <dc:creator>Hello_there</dc:creator>
      <dc:date>2022-12-02T14:25:25Z</dc:date>
    </item>
    <item>
      <title>Re: SPARSE option in proc freq. Is it possible to only include all possible values of one variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847509#M335075</link>
      <description>&lt;P&gt;I've just expanded my previous post by an explanation why &lt;FONT face="courier new,courier"&gt;group by 2, 3, 1&lt;/FONT&gt; is different from &lt;FONT face="courier new,courier"&gt;group by color, brand, trt&lt;/FONT&gt;.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Dec 2022 21:20:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847509#M335075</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-12-02T21:20:04Z</dc:date>
    </item>
    <item>
      <title>Re: SPARSE option in proc freq. Is it possible to only include all possible values of one variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847529#M335078</link>
      <description>Thanks again for your help, FreelanceReinhard!&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sat, 03 Dec 2022 01:48:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847529#M335078</guid>
      <dc:creator>Hello_there</dc:creator>
      <dc:date>2022-12-03T01:48:33Z</dc:date>
    </item>
    <item>
      <title>Re: SPARSE option in proc freq. Is it possible to only include all possible values of one variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/848865#M335607</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/sqlproc/p12ohgh32ffm6un13s7l2d5p9c8y.htm#n0dl95cpzwue89n1iobwxfguflpz" target="_blank" rel="noopener"&gt;FEEDBACK option&lt;/A&gt; of the PROC SQL statement reveals what happens inside the natural join:&lt;/P&gt;
&lt;PRE&gt;NOTE: The execution of this query involves performing one or more Cartesian product joins that can not be optimized.
NOTE: The query requires remerging summary statistics back with the original data.
NOTE: Table WORK.WANT created, with 12 rows and 4 columns.&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Hi FreelanceReinhard,&lt;/P&gt;
&lt;P&gt;Do you know if there's any chance how i can avoid the top 2 notes?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Dec 2022 09:11:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/848865#M335607</guid>
      <dc:creator>Hello_there</dc:creator>
      <dc:date>2022-12-10T09:11:44Z</dc:date>
    </item>
    <item>
      <title>Re: SPARSE option in proc freq. Is it possible to only include all possible values of one variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/848885#M335625</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/252358"&gt;@Hello_there&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;PRE&gt;NOTE: The execution of this query involves performing one or more Cartesian product joins that can not be optimized.
NOTE: The query requires remerging summary statistics back with the original data.
NOTE: Table WORK.WANT created, with 12 rows and 4 columns.&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The second of these notes does not occur with the code from the accepted solution. It occurred with the PROC SQL step that I used to demonstrate that the correct GROUP BY clause&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;group by 2,3,1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;from the solution is not equivalent to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;group by color, brand, trt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So you may be using the wrong code if you see that note with your final program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Generally speaking, if the goal is to have a clean log, then you don't need to do anything about these notes as they do not indicate problems. They just describe the internal workings of a PROC SQL step to a level of detail that we are not used to with other procedures like PROC FREQ or PROC SUMMARY.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you &lt;EM&gt;must&lt;/EM&gt; avoid this type of notes, you can either apply a cosmetic change to the log or replace the concise PROC SQL step with a more cumbersome multi-step solution, possibly avoiding PROC SQL altogether (at least Cartesian product joins and automatic remerging). Example: the approach using concatenated values in a variable &lt;FONT face="courier new,courier"&gt;comb&lt;/FONT&gt; as outlined in the first sentence of the accepted solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For a "cosmetic" solution there are two options, neither of which I would recommend:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Suppress &lt;EM&gt;all&lt;/EM&gt; notes from the PROC SQL step by means of &lt;FONT face="courier new,courier"&gt;options nonotes&lt;/FONT&gt; and then write the desired notes to the log:
&lt;PRE&gt;&lt;STRONG&gt;options nonotes;&lt;/STRONG&gt;
&lt;FONT color="#999999"&gt;proc sql;
create table want as
select *, count(a.trt) as count from 
have a
natural right join
(select distinct * from have(keep=trt), have(drop=trt))
group by 2,3,1;
quit;&lt;/FONT&gt;

&lt;STRONG&gt;options notes nosource;
%let _dsid=%sysfunc(open(&amp;amp;syslast));
%let _nvar=%sysfunc(attrn(&amp;amp;_dsid,NVARS));
%let _dsid=%sysfunc(close(&amp;amp;_dsid));
%put NOTE: Table %cmpres(&amp;amp;syslast) created, with &amp;amp;sqlobs rows and &amp;amp;_nvar columns.;
%symdel _dsid _nvar;
proc sql;
quit;
options source;&lt;/STRONG&gt;&lt;/PRE&gt;
&lt;P&gt;Log:&lt;/P&gt;
&lt;PRE&gt;12    options nonotes;
13    proc sql;
14    create table want as
15    select *, count(a.trt) as count from
16    have a
17    natural right join
18    (select distinct * from have(keep=trt), have(drop=trt))
19    group by 2,3,1;
20    quit;
21
22    options notes nosource;
&lt;FONT color="#3366FF"&gt;NOTE: Table WORK.WANT created, with 12 rows and 4 columns.
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/LI&gt;
&lt;LI&gt;Read, manipulate and rewrite the log as a text file.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Sat, 10 Dec 2022 13:05:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/848885#M335625</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-12-10T13:05:05Z</dc:date>
    </item>
    <item>
      <title>Re: SPARSE option in proc freq. Is it possible to only include all possible values of one variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/848924#M335639</link>
      <description>Thanks for explaining.&lt;BR /&gt;Learning about how useful natural joins are have been useful for my use cases so this was a definitely helpful.&lt;BR /&gt;You were right, i was running the wrong code before and that's where the one note came from bc i didn't use a group by statement.&lt;BR /&gt;As for having a clean log, I might have to get used to seeing "Note:" in my log. Usually we have a log checker, and it might flag for just warnings and errors and not notes.</description>
      <pubDate>Sat, 10 Dec 2022 19:29:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/848924#M335639</guid>
      <dc:creator>Hello_there</dc:creator>
      <dc:date>2022-12-10T19:29:53Z</dc:date>
    </item>
    <item>
      <title>Re: SPARSE option in proc freq. Is it possible to only include all possible values of one variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/854350#M337637</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt; ,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't know if you have any experience w/ dealing w/ clinical trial data, but I'm trying to use this code to get counts for distinct trios of SOC term, PT term, and TRT01AN. Not every TRT01AN is in the main data set, so i had to pull them from another data set. When I run the below code, i get duplicates. After the right join, i'm trying to combine distinct TRT01AN values, with distinct pairs of SOC term and PT terms. Do you have a suggestion for how I can do that in one PROC SQL step? I know it's possible do a proc sort and use nodup to fix my issue, but i was wondering if i could just do it 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 want as
select pt_term, soc_term, trt01an, count(distinct a.subject) as count from 
adae a
natural right join
(select distinct * from adae(keep=soc_term pt_term), adsl(keep=trt01an))
group by soc_term, pt_term, trt01an;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Jan 2023 17:29:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/854350#M337637</guid>
      <dc:creator>Hello_there</dc:creator>
      <dc:date>2023-01-18T17:29:09Z</dc:date>
    </item>
    <item>
      <title>Re: SPARSE option in proc freq. Is it possible to only include all possible values of one variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/854364#M337642</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/252358"&gt;@Hello_there&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you still get duplicates after changing the GROUP BY clause to&lt;/P&gt;
&lt;PRE&gt;group by soc_term, pt_term, &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;calculated&lt;/STRONG&gt;&lt;/FONT&gt; trt01an;&lt;/PRE&gt;
&lt;P&gt;(see&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/847429/highlight/true#M335035" target="_blank" rel="noopener"&gt;this earlier post&lt;/A&gt; in this thread for the reason)?&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jan 2023 16:05:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/854364#M337642</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-01-18T16:05:00Z</dc:date>
    </item>
    <item>
      <title>Re: SPARSE option in proc freq. Is it possible to only include all possible values of one variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/854371#M337647</link>
      <description>&lt;P&gt;Unfortunately that didn't work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If i break it apart this code returns back the right number of rows (without the counts).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table look as
	select * from 
  		(select distinct trt01an from adsl),
  		(select distinct soc_term, pt_term from adae);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I was wondering if it was possible to do a right join with the counts to that. But it looks like the below code doesn't work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
	select soc_term, pt_term, trt01an, count(distinct a.subject) as count
	from adae a
	natural right join
 		select * from
			(select distinct trt01an from adsl),
  			(select distinct soc_term, pt_term from adae)
	group by soc_term, pt_term, trt01an;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jan 2023 16:37:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/854371#M337647</guid>
      <dc:creator>Hello_there</dc:creator>
      <dc:date>2023-01-18T16:37:43Z</dc:date>
    </item>
    <item>
      <title>Re: SPARSE option in proc freq. Is it possible to only include all possible values of one variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/854377#M337649</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/252358"&gt;@Hello_there&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Unfortunately that didn't work.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So, you still got duplicates? Or an error in the log (if so, please show the log)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;SPAN&gt;If i break it apart this code returns back the right number of rows (without the counts).&lt;/SPAN&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table look as
	select * from 
  		(select distinct trt01an from adsl),
  		(select distinct soc_term, pt_term from adae);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I was wondering if it was possible to do a right join with the counts to that. But it looks like the below code doesn't work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
	select soc_term, pt_term, trt01an, count(distinct a.subject) as count
	from adae a
	natural right join
 		select * from
			(select distinct trt01an from adsl),
  			(select distinct soc_term, pt_term from adae)
	group by soc_term, pt_term, trt01an;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;To use the first query (creating dataset &lt;FONT face="courier new,courier"&gt;look&lt;/FONT&gt;) as a subquery in another query you must put it into parentheses: ... &lt;FONT face="courier new,courier"&gt;right join &lt;FONT size="4"&gt;&lt;STRONG&gt;(&lt;/STRONG&gt;&lt;/FONT&gt;select * from (...), (...)&lt;FONT size="4"&gt;&lt;STRONG&gt;)&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your query has changed from your previous post, which used&amp;nbsp;&lt;FONT face="courier new,courier"&gt;aebodsys&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;aedecod&lt;/FONT&gt;. Now that &lt;FONT face="courier new,courier"&gt;soc_term&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;pt_term&lt;/FONT&gt; come from both sides of the natural right join, you would need the "&lt;FONT face="courier new,courier"&gt;calculated&lt;/FONT&gt;" keyword before &lt;EM&gt;all three&lt;/EM&gt; items in the GROUP BY clause or simply use the abbreviation&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;group by 1,2,3;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;as discussed earlier in this thread.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jan 2023 17:03:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/854377#M337649</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-01-18T17:03:16Z</dc:date>
    </item>
    <item>
      <title>Re: SPARSE option in proc freq. Is it possible to only include all possible values of one variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/854387#M337654</link>
      <description>Your advice worked! Adding the () around the subquery and the calculated before each group by term worked. Thanks again!&lt;BR /&gt;&lt;BR /&gt;I used soc_term/aebodsys and pc_term/aedecod interchangeably; i should have been more consistent, sorry about the confusion. &lt;BR /&gt;&lt;BR /&gt;Also as for the first question, there were no errors in the log, but it was most likely due to the variable mix up on my part that caused the duplicates.</description>
      <pubDate>Wed, 18 Jan 2023 17:38:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SPARSE-option-in-proc-freq-Is-it-possible-to-only-include-all/m-p/854387#M337654</guid>
      <dc:creator>Hello_there</dc:creator>
      <dc:date>2023-01-18T17:38:29Z</dc:date>
    </item>
  </channel>
</rss>

