Help using Base SAS procedures

Proc Freq--All inclusive table

Accepted Solution Solved
Reply
Contributor
Posts: 45
Accepted Solution

Proc Freq--All inclusive table

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


Accepted Solutions
Solution
‎07-20-2015 03:34 PM
Respected Advisor
Posts: 3,777

Re: Proc Freq--All inclusive table

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


All Replies
Super User
Posts: 9,662

Re: Proc Freq--All inclusive table

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

Contributor
Posts: 45

Re: Proc Freq--All inclusive table

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.

Super User
Posts: 17,750

Re: Proc Freq--All inclusive table

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. 

Solution
‎07-20-2015 03:34 PM
Respected Advisor
Posts: 3,777

Re: Proc Freq--All inclusive table

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
Contributor
Posts: 45

Re: Proc Freq--All inclusive table

Thank you all so much! Smiley Happy

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 421 views
  • 7 likes
  • 4 in conversation