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

I need to create a new variable (count_var) that counts the number of questions that were left unanswered in a survey. The value of an unanswered question is 0. There are 10 questions in the survey. 

 

Ideally, the value of count_var will equal the number of unanswered questions for each subject.

 

I was not abCreate an enumeration variable with a condition" post in this forum. 

 

Thanks for your assistance!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

If the values for non-answered had been treated as MISSING instead of assigned a value this would be trivial with the CMISS function.

I strongly recommend considering use of missing values instead of 0 as you cannot expect to get really accurate values for things like "average response" or most statistics with your current coding.

However Assuming the values are 1) actually numeric 2) one observation per respondent then something like

Data want;

   set have;

   array q q1-q10; /* assumes the questions are named Q1 Q2 etc. put list of actual names if significantly different*/

   Count_var=0;

   do i=1 to dim(q);

      count_var= count_var + (q[i] = 0);

   end;

   drop i;

run;

 

 

If your variables are actually character then use = "0".

If we knew the range of of the responses it might be possible to get the result in a single line use COUNTC and a contactenation of the values, but insufficient information of the values was provided.

View solution in original post

5 REPLIES 5
LinusH
Tourmaline | Level 20
Are each question/answer on separate observations?

Proc sql;
Create table want as
Select survey, subject, count (*) as count_var
From have
Where answer is null;
Quit;
Data never sleeps
_maldini_
Barite | Level 11

<Are each question/answer on separate observations?>

 

Yes, there are 10 questions. The values of the observations can be between 0 and 3.

 

I'm a relatively novice SAS user and have never used PROC SQL. A few clarifying questions:

 

<Create table want as>

 

This is creating a table with the desired items? What does the "as" command do?

 

<Select survey, subject, count (*) as count_var>

 

Is survey a list of variables that make up the survey?

 

<Where answer is null;>

 

How does SAS define "null"?

 

Thanks!!!

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Please post sample test data (in the form of a datastep) and required output otherwise code will just be guessing.

ballardw
Super User

If the values for non-answered had been treated as MISSING instead of assigned a value this would be trivial with the CMISS function.

I strongly recommend considering use of missing values instead of 0 as you cannot expect to get really accurate values for things like "average response" or most statistics with your current coding.

However Assuming the values are 1) actually numeric 2) one observation per respondent then something like

Data want;

   set have;

   array q q1-q10; /* assumes the questions are named Q1 Q2 etc. put list of actual names if significantly different*/

   Count_var=0;

   do i=1 to dim(q);

      count_var= count_var + (q[i] = 0);

   end;

   drop i;

run;

 

 

If your variables are actually character then use = "0".

If we knew the range of of the responses it might be possible to get the result in a single line use COUNTC and a contactenation of the values, but insufficient information of the values was provided.

_maldini_
Barite | Level 11

Thank you so much for your help. This code worked!

 

All I needed to do was add (*) to the ARRAY statement:

 

< array q (*)  q1-q10>

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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