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

Is it possible to proc freq output that is something like this 

Number of participants (%)

Number of surveys 

1030 (27)

1

100 (24)

2

 

 

instead of the regular output which is down below. 

 

               Frequency Count

Number of surveys

Frequency

Percent

1

1030

27

2

100

24

 

I was told you can use proc freq and the use proc print (w/ formatting) but, am confused. Please help!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Why is the question subject "Proc Print" when the question is for Proc Freq.

 

To have multiple values in a single "cell" you will need to make a single value in a dataset, which Proc Freq and basically none of the procedures will do, and then use something else to display the result, such as Proc Print or Report.

 

So you would use proc freq to create an output data set and then do something like:

Assumes the data set you created from Proc freq is named "have".

data want;
   set have;
   length Participants $ 15;
   participants = catx(' ',Count,cats('(',put(percent,f3.0),')'));
   label participants='Number of participants (%)';
run;

proc print data=want noobs label;
  var participants surveys;
run;

View solution in original post

7 REPLIES 7
ballardw
Super User

Why is the question subject "Proc Print" when the question is for Proc Freq.

 

To have multiple values in a single "cell" you will need to make a single value in a dataset, which Proc Freq and basically none of the procedures will do, and then use something else to display the result, such as Proc Print or Report.

 

So you would use proc freq to create an output data set and then do something like:

Assumes the data set you created from Proc freq is named "have".

data want;
   set have;
   length Participants $ 15;
   participants = catx(' ',Count,cats('(',put(percent,f3.0),')'));
   label participants='Number of participants (%)';
run;

proc print data=want noobs label;
  var participants surveys;
run;
hjjijkkl
Pyrite | Level 9

Am still having errors. I did created an output using proc freq which looks like the data below 

Number of Survey  Freq Percent 
2 30 2.5
5 50 30.9

from here, I have followed what recommended below but I am not able to get the table I wanted. 

data want;
   set have;
   length Participants $ 15;
   participants = catx(' ',frequency,cats('(',put(percent,f3.0),')'));
   label participants='Number of participants (%)';
run;

proc print data=want noobs label;
  var participants surveys;
run;,  

ballardw
Super User

So, what is not correct with the output???? Details matter.

 

Doesn't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the <> to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the <> icon or attached as text to show exactly what you have and that we can test code against.

 

You have changed the values but are not showing the expected output.

And my variables may not match your names because you did not provide variable names.

 

Provide examp

hjjijkkl
Pyrite | Level 9
Thank you!
There is still an issue. the first column "Number of Survey" do I add that to proc print? And How? Please the data below for reference.
this is what am proc freq "have" look like
Number of Survey Freq Percent
1 30 2.5
2 50 30.9

This is how I would want the table to look like at the end
Number of participants (%) Number of surveys
1030 (27) 1
100 (24) 2







hjjijkkl
Pyrite | Level 9

Sorry. it wasn't error that i was getting but, this is the print out after following your guidance. Thank you!

Number of
participants
(%)

. (27)

. (25)

. (17)

 
 
 
 
ballardw
Super User

use Count  not Frequency in the CATX statement.

 

Check the names of your varaibles in a data set. Always.

Reeza
Super User

You cannot do this within PROC FREQ. You can customize your formats, layout and measures in PROC TABULATE more easily but it still won't give you exactly what you want there. The approach to generate those types of tables is to put your results from FREQ into a data set, create the variables you want and how you want to display them and then print that data set. 

 

Another option is to use macros that other people have written but you're then stuck with their options and preferences so usually I write my own to suit my preferences. 

 

https://communities.sas.com/t5/SAS-Communities-Library/Demographic-Table-and-Subgroup-Summary-Macro-...

 

For a one way table here's one method, a bit longer for sure though. 

https://gist.github.com/statgeek/e0903d269d4a71316a4e

/*This code is an example of how to generate a table with 
Variable Name, Variable Value, Frequency, Percent, Cumulative Freq and Cum Pct
No macro's are required
Use Proc Freq to generate the list, list variables in a table statement if only specific variables are desired
Use ODS Table to capture the output and then format the output into a printable table.
*/

*Run frequency for tables;
ods table onewayfreqs=temp;
proc freq data=sashelp.class;
	table sex age;
run;

*Format output;
data want;
length variable $32. variable_value $50.;
set temp;
Variable=scan(table, 2);

Variable_Value=strip(trim(vvaluex(variable)));

keep variable variable_value frequency percent cum:;
label variable='Variable' 
	variable_value='Variable Value';
run;

*Display;
proc print data=want(obs=20) label;
run;

And examples of custom macros:

https://gist.github.com/statgeek/c099e294e2a8c8b5580a

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 1922 views
  • 2 likes
  • 3 in conversation