DATA Step, Macro, Functions and more

Create an enumeration variable with a condition

Accepted Solution Solved
Reply
Regular Contributor
Posts: 199
Accepted Solution

Create an enumeration variable with a condition

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!

 

 


Accepted Solutions
Solution
‎03-02-2016 01:35 PM
Super User
Posts: 11,338

Re: Create an enumeration variable with a condition

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


All Replies
Super User
Posts: 5,424

Re: Create an enumeration variable with a condition

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
Regular Contributor
Posts: 199

Re: Create an enumeration variable with a condition

<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!!!

Super User
Super User
Posts: 7,942

Re: Create an enumeration variable with a condition

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

Solution
‎03-02-2016 01:35 PM
Super User
Posts: 11,338

Re: Create an enumeration variable with a condition

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.

Regular Contributor
Posts: 199

Re: Create an enumeration variable with a condition

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>

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 436 views
  • 3 likes
  • 4 in conversation