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

Good afternoon,

I have a question related to the tabular output of proc freq.

Let's say that I have the following survey question:

Do you like politics?

a) Yes

b) No

c) Maybe

d) Uncertain

In the event that one of the 4 options gets selected by nobody (say d), Proc freq will give me the following table (Code created for your convenience):

data example;

input answer $ @@;

datalines;

Yes Yes Yes Yes Yes No Maybe Maybe Maybe No No Yes

run;

proc freq data=example;

run;

answerFrequencyPercentCumulative FrequencyCumulative Percent
Maybe325325
No325650
Yes65012100

While I would like the missing 'd. Uncertain' to be recorded:

answerFrequencyPercentCumulative FrequencyCumulative Percent
Maybe325325
Uncertain00325
No325650
Yes65012100

Do you know how can I make proc freq output the second table? If proc freq can't do it, do you have a different procedure in mind that can?

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19
data classdata;
   input answer :$10.;
  
cards;
Yes
No
Maybe
Uncertain
;;;;
  
run;
proc print;
  
run;
data example;
   input answer :$10. @@;
   datalines;
   Yes Yes Yes Yes Yes No Maybe Maybe Maybe No No Yes
   run;
proc print;
  
run;
proc summary data=example nway classdata=classdata order=data;
   class answer;
   output out=freq(drop=_type_);
   run;
proc print;
  
run;
proc freq data=freq order=data;
   tables answer;
   weight _freq_ / zeros;
  
run;

Capture.PNG

View solution in original post

5 REPLIES 5
Ksharp
Super User

Simple Data Step ?

Code: Program

data example;
input answer $ @@;
datalines;
Yes Yes Yes Yes Yes No Maybe Maybe Maybe No No Yes
run;

ods output OneWayFreqs=temp;
proc freq data=example ;
tables answer/ cumcol;
run;
data want;
length answer $ 20;
set temp;
output;
if _n_ eq 1 then do;
  answer='Uncertain';
  Frequency=0;Percent=0;output;
end;
drop Table F_answer;
run;

Xia Keshan

Greek
Obsidian | Level 7

Hello Xia,

Thank you very much for your response. I was hoping for something less manual as I need to produce a large series of frequency tables and I am using macros, but this is certainly helpful. I will use it in a case-by-case basis.

Reeza
Super User

Look into proc tabulate and PRELOADFMT

There are probably more clear examples than this one, but its the documentation Smiley Happy

25400 - Use of PRELOADFMT with PROC TABULATE

No matter what, you need some table or somewhere that has the full list of all options. 

data_null__
Jade | Level 19
data classdata;
   input answer :$10.;
  
cards;
Yes
No
Maybe
Uncertain
;;;;
  
run;
proc print;
  
run;
data example;
   input answer :$10. @@;
   datalines;
   Yes Yes Yes Yes Yes No Maybe Maybe Maybe No No Yes
   run;
proc print;
  
run;
proc summary data=example nway classdata=classdata order=data;
   class answer;
   output out=freq(drop=_type_);
   run;
proc print;
  
run;
proc freq data=freq order=data;
   tables answer;
   weight _freq_ / zeros;
  
run;

Capture.PNG
Greek
Obsidian | Level 7

Thank you all so much! Smiley Happy

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2288 views
  • 7 likes
  • 4 in conversation