BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Charlie
Fluorite | Level 6

Dear all

 

I am trying to create a cross table to see how food test change, I used following code:

 

data raw;
  input person firsttest $ secondtest $;
  datalines;
  1 Good Good
  2 Poor Good
  3 Good Bad
  4 Poor Bad
  5 Poor Poor
;
run;

 

proc freq data=raw;
  table firsttest*secondtest/nocol norow nopercent;
run;

 

due to results in firttest dose not have "Bad" result, therefore, it shows:

 

Table of firsttest by secondtest
 secondtest
firsttestBadGoodPoorTotal
Good1102
Poor1113
Total2215

 

I am wondering how to display a full result as following table:

 

Table of firsttest by secondtest
 secondtest
firsttestBadGoodPoorTotal
 Bad0000
Good1102
Poor1113
Total2215

I also tried to use prco tabulate to do it, but it failed.

 

Appreciate the help.

 

 

Charlie

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data raw;
  input person firsttest $ secondtest $;
  datalines;
  1 Good Good
  2 Poor Good
  3 Good Bad
  4 Poor Bad
  5 Poor Poor
;
run;
data have;
 set raw end=last;
 w=1;output;
 if last then do;w=0;firsttest='Bad';output;end;
run;
 

proc freq data=have;
  table firsttest*secondtest/nocol norow nopercent;
  weight w/zero;
run;

View solution in original post

5 REPLIES 5
Reeza
Super User

PRELOADFMT is usually one way. Basically, if it's not in the table you have to tell SAS in some manner that it needs to exist, otherwise it can't know that you want to add something that doesn't exist in the data. 

 

/*This demonstrates how to ensure all levels are in your report
using the preloadfmt option*/


data raw;
  input person firsttest $ secondtest $;
  datalines;
  1 Good Good
  2 Poor Good
  3 Good Bad
  4 Poor Bad
  5 Poor Poor
;
run;

proc format;
value $ test_values 
   "Good" = "Good"
   "Poor" = "Poor"
   "Bad" = "Bad";
 run;
 

proc tabulate data=raw;
class firsttest secondtest / preloadfmt;
table firsttest, secondtest / misstext='0' printmiss;
format firsttest secondtest $test_values.;
run;

@Charlie wrote:

Dear all

 

I am trying to create a cross table to see how food test change, I used following code:

 

data raw;
  input person firsttest $ secondtest $;
  datalines;
  1 Good Good
  2 Poor Good
  3 Good Bad
  4 Poor Bad
  5 Poor Poor
;
run;

 

proc freq data=raw;
  table firsttest*secondtest/nocol norow nopercent;
run;

 

due to results in firttest dose not have "Bad" result, therefore, it shows:

 

Table of firsttest by secondtest
  secondtest
firsttest Bad Good Poor Total
Good 1 1 0 2
Poor 1 1 1 3
Total 2 2 1 5

 

I am wondering how to display a full result as following table:

 

Table of firsttest by secondtest
  secondtest
firsttest Bad Good Poor Total
 Bad 0 0 0 0
Good 1 1 0 2
Poor 1 1 1 3
Total 2 2 1 5

I also tried to use prco tabulate to do it, but it failed.

 

Appreciate the help.

 

 

Charlie


 

Charlie
Fluorite | Level 6
Thank you for your reply, I am not so familiar with proc tabulate and learn from your reply, I will do more homework on it.
Ksharp
Super User
data raw;
  input person firsttest $ secondtest $;
  datalines;
  1 Good Good
  2 Poor Good
  3 Good Bad
  4 Poor Bad
  5 Poor Poor
;
run;
data have;
 set raw end=last;
 w=1;output;
 if last then do;w=0;firsttest='Bad';output;end;
run;
 

proc freq data=have;
  table firsttest*secondtest/nocol norow nopercent;
  weight w/zero;
run;
Charlie
Fluorite | Level 6
This is exactly what I want, thank you a lot.
PhilC
Rhodochrosite | Level 12

It would be a great thing if we could allow the original poster to mark more than one response as a solution. 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2353 views
  • 0 likes
  • 4 in conversation