Professional > Data management scripting > Creating new variables > Example 2: Creating a numeric variable to store the total number of questions answered
 
Example 2: Creating a numeric variable to store the total number of questions answered
Like the second example in Example 1: Creating a categorical variable from a numeric variable, the case data for the variable in this example is set up in the OnNextCase Event section. However, this time it is a numeric (Long) variable that stores the number of questions that have been answered.
Here is the Metadata section, which creates the numeric variable in the output metadata:
Metadata(ENU, Question, Label, myInputDataSource)
QuestionsAnswered "Number of questions answered" long;
End Metadata
Here is the mrScriptBasic code for the OnNextCase Event Section. This code uses a For Each...Next loop and a Function procedure to count the number of questions that have been answered (or more precisely, the number of variables that store responses).
Event(OnNextCase, "Set up the case data")
Dim Question

For Each Question in dmgrQuestions
QuestionsAnswered = QuestionsAnswered + GetQuestionAnsweredCount(Question)
Next

Function GetQuestionAnsweredCount(Question)
If Question.Count > 0 Then
Dim Counter
For Counter = 0 to Question.Count - 1
GetQuestionAnsweredCount = GetQuestionAnsweredCount + GetQuestionAnsweredCount(Question[Counter])
Next
Else
If Question IS NOT NULL Then
GetQuestionAnsweredCount = 1
Else
GetQuestionAnsweredCount = 0
End If
End If
End Function
End Event
Note The examples in this topic are included in the NewVariables.dms sample DMS file. For more information, see Sample DMS files.
Requirements
UNICOM Intelligence Professional
See also
Creating new variables