BookmarkSubscribeRSS Feed
SAS-questioner
Obsidian | Level 7

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:

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  

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:

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   

Because cat2 doesn't have 3, so the count for it always be 0. I tried to use 

proc freq;
table cat1*cat2/list;
run;

But this code doesn't create the "3" if cat2 doesn't have it. Could any one help me with it? Thank you!

3 REPLIES 3
Patrick
Opal | Level 21

With Proc Freq you would need to create rows for the missing combinations and add it to the table - which is not pretty.

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.

Example 2: Specifying Class Variable Combinations to Appear in a Table

mkeintz
PROC Star

You could add a collection of records covering each of the cat1/cat2 combinations.  But assign a weight of zero to this additional obs, and a weight of one to the originals.  As in:

 

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;

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Tom
Super User Tom
Super User

Some how you have to tell SAS that 3 was a possible answer.

PROC SUMMARY will let you do that by attaching a FORMAT to the variable.

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;

Result

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 396 views
  • 0 likes
  • 4 in conversation