BookmarkSubscribeRSS Feed
pbstubejunkie
Calcite | Level 5

Hello all,

 

I'm working on a project where I need to count the total responses for each variable. The variables represent responses to different survey questions and I just neet to know how many people responded to each questoin. The total number of responses for each question will then be used as the numerator with the total number of observations as the denominator. Is there a shortcut to doing this? I have over 300 variables and 180,000 observatioins, I would rather not have to hard code all of this. 

2 REPLIES 2
Astounding
PROC Star

Well, you haven't been specific about what form the output should take.  Here's a reasonable and easy way:

 

proc summary data=have;

var _numeric_;

output out=want (drop=_type_ _freq_) n=;

run;

 

It gives you one observation, where the value of each numeric variable is the number of non-missing values from the original data set.  Perhaps that is just right, or perhaps you can use it as a starting point to clarify what you would like the results to look like.

ballardw
Super User

@pbstubejunkie wrote:

Hello all,

 

The total number of responses for each question will then be used as the numerator with the total number of observations as the denominator.


That looks a lot like a percentage. You haven't stated whether you need the result in a dataset or a report.

Proc freq will do that percent calculation:

 

proc freq data=have ;
   tables _character_ _numeric_/missing nocum;
run;

The tables statement says to use all the character and numeric variables. Missing says to include the missing/blank values as a category so you will get percentages based on all records when calculating the percent for each response. The nocum suppresses a cumulative percentage output. The tables statement is needed because otherwise Proc Freq will exclude the missing values from denominator.

 

If you need a data set you could add and ods output statement

proc freq data=have ;
   tables _character_ _numeric_/missing nocum;
   ods output onewayfreqs=work.freqs;
run;

though that may take some massaging for some other uses because you will have columns for the variable values and the formatted values as columns along with the count and percentages and a column to indicate which "table", or variable was summarized.

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 2281 views
  • 0 likes
  • 3 in conversation