<?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 How to create a count variables for the combination of two categorical variables? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-variables-for-the-combination-of-two/m-p/921418#M362872</link>
    <description>&lt;P&gt;Hi, I have a two categorical variables (1 to 4). the two categorical are supposed to be the same, but some people didn't chose parts of the answers, that make the second categorical variable has less categories than the first one. The data is like below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ID  cat1    cat2
 1    1       1
 2    1       2
 3    1       4
 4    2       1
 5    2       2
 6    2       4
 7    3       1
 8    3       2
 9    3       4
10    4       1
11    4       2
12    4       4  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Because no one answered '3' for the cat2, it looks like cat2 only has three categories. I want to create a count data, but also include the '3' for cat2. Below are the data that I wanted:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;cat1 cat2  count
  1     1        1
  1     2        1
  1     3        0
  1    4         1
  2    1         1
  2    2         1
  2    3         0
  2    4         1
  3    1         1
  3    2         1
  3   3          0
  3    4         1
  4    1         1
  4    2         1
  4    3         0
  4    4         1   &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Because cat2 doesn't have 3, so the count for it always be 0. I tried to use&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq;
table cat1*cat2/list;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But this code doesn't create the "3" if cat2 doesn't have it. Could any one help me with it? Thank you!&lt;/P&gt;</description>
    <pubDate>Thu, 21 Mar 2024 22:25:52 GMT</pubDate>
    <dc:creator>SAS-questioner</dc:creator>
    <dc:date>2024-03-21T22:25:52Z</dc:date>
    <item>
      <title>How to create a count variables for the combination of two categorical variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-variables-for-the-combination-of-two/m-p/921418#M362872</link>
      <description>&lt;P&gt;Hi, I have a two categorical variables (1 to 4). the two categorical are supposed to be the same, but some people didn't chose parts of the answers, that make the second categorical variable has less categories than the first one. The data is like below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ID  cat1    cat2
 1    1       1
 2    1       2
 3    1       4
 4    2       1
 5    2       2
 6    2       4
 7    3       1
 8    3       2
 9    3       4
10    4       1
11    4       2
12    4       4  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Because no one answered '3' for the cat2, it looks like cat2 only has three categories. I want to create a count data, but also include the '3' for cat2. Below are the data that I wanted:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;cat1 cat2  count
  1     1        1
  1     2        1
  1     3        0
  1    4         1
  2    1         1
  2    2         1
  2    3         0
  2    4         1
  3    1         1
  3    2         1
  3   3          0
  3    4         1
  4    1         1
  4    2         1
  4    3         0
  4    4         1   &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Because cat2 doesn't have 3, so the count for it always be 0. I tried to use&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq;
table cat1*cat2/list;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But this code doesn't create the "3" if cat2 doesn't have it. Could any one help me with it? Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 21 Mar 2024 22:25:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-variables-for-the-combination-of-two/m-p/921418#M362872</guid>
      <dc:creator>SAS-questioner</dc:creator>
      <dc:date>2024-03-21T22:25:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a count variables for the combination of two categorical variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-variables-for-the-combination-of-two/m-p/921424#M362875</link>
      <description>&lt;P&gt;With Proc Freq you would need to create rows for the missing combinations and add it to the table - which is not pretty.&lt;/P&gt;
&lt;P&gt;Proc Tabulate is one of the procedures that allows you to use a 2nd data set that defines all combinations of values for class variables that you want to see in a report.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/proc/n18cg5nn785mrnn1semjee5eaywe.htm#n18cg5nn785mrnn1semjee5eaywe" target="_self"&gt;Example 2: Specifying Class Variable Combinations to Appear in a Table&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Mar 2024 23:31:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-variables-for-the-combination-of-two/m-p/921424#M362875</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-03-21T23:31:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a count variables for the combination of two categorical variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-variables-for-the-combination-of-two/m-p/921425#M362876</link>
      <description>&lt;P&gt;You could add a collection of records covering each of the cat1/cat2 combinations.&amp;nbsp; But assign a weight of zero to this additional obs, and a weight of one to the originals.&amp;nbsp; As in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID  cat1    cat2;
datalines;
 1    1       1
 2    1       2
 3    1       4
 4    2       1
 5    2       2
 6    2       4
 7    3       1
 8    3       2
 9    3       4
10    4       1
11    4       2
12    4       4
run;

data vtemp/view=vtemp;
  set have end=end_of_have;
  retain wgt 1;
  output;
  if end_of_have;
  wgt=0;
  do cat1=1 to 4; do cat2=1 to 4; output; end; end;
run;

proc freq data=vtemp;
  table cat1*cat2 / nopercent norow nocol;
  weight wgt /zeroes;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note you have to make a WGT variable (=1 for the originals and 0 for the dummy additions) and then use it in the WEIGHT statement.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Mar 2024 03:33:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-variables-for-the-combination-of-two/m-p/921425#M362876</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-03-22T03:33:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a count variables for the combination of two categorical variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-variables-for-the-combination-of-two/m-p/921427#M362878</link>
      <description>&lt;P&gt;Some how you have to tell SAS that 3 was a possible answer.&lt;/P&gt;
&lt;P&gt;PROC SUMMARY will let you do that by attaching a FORMAT to the variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format ;
value cat 
 1='1' 2='2' 3='3' 4='4' 
;
run;

proc summary data=have nway completetypes;
  class cat1 cat2 / preloadfmt;
  format cat1 cat2 cat.;
  output out=counts ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    cat1    cat2    _TYPE_    _FREQ_

  1     1       1         3         1
  2     1       2         3         1
  3     1       3         3         0
  4     1       4         3         1
  5     2       1         3         1
  6     2       2         3         1
  7     2       3         3         0
  8     2       4         3         1
  9     3       1         3         1
 10     3       2         3         1
 11     3       3         3         0
 12     3       4         3         1
 13     4       1         3         1
 14     4       2         3         1
 15     4       3         3         0
 16     4       4         3         1
&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Mar 2024 00:25:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-count-variables-for-the-combination-of-two/m-p/921427#M362878</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-03-22T00:25:56Z</dc:date>
    </item>
  </channel>
</rss>

