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. 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 1812 views
  • 0 likes
  • 4 in conversation