Desktop User Guides > Professional > Interview scripting > Writing interview scripts > Working with sample records > Converting data to the required format
 
Converting data to the required format
Data is not always in the right format for the situation in which you want to use it: the sample field might have gender coded numerically, for example, while you want to read it into a categorical question, or you might have categorical data that you have to write back into a numeric field. If you have only a few conversions of this type, you can specify them separately for each question or field, but an alternative is to write a function that you can call generically whenever you need to perform a certain type of conversion.
Converting numeric sample data into categorical script data
The following example uses the numeric to categorical conversion for gender as an example. If you have a number of similar fields, it makes sense to write a numeric to categorical conversion function that you can use for any question. The Gender and Mstat questions are defined in the script as:
Gender "Gender" categorical [1..1]
{Male, Female};
Mstat "Please confirm your marital status" categorical [1..1]
{
Single, Married, Divorced, Widowed,
Partner "Living with partner"
};
However, the “answers” to these questions are stored in the sample records in two numeric fields. Gender is stored as 1 or 2 in SampleGender and marital status is a value in the range 1 to 5 in SampleMstat. For simplicity, assume that there are no missing or incorrect values: everyone’s gender and marital status is known and correctly coded. You therefore need a function that can convert numeric values and assign them as the answers to categorical questions. The following function does this:
Sub NumSamp2CatQ(Question, FieldValue)
FieldValue = CLong(FieldValue)
If FieldValue >= 1 And FieldValue <= Question.Categories.Count Then
Question.Response.Value = Question.Categories.Item[FieldValue-1].Value
End If
End Sub
The function is passed a question name and the value in the corresponding sample field. It converts the value to an integer and checks that it is within the number of responses in the question’s response list. If it is, the function uses that value as an index into the response list and selects the name (category value) of the response in that position in the list. (Responses are numbered from zero, so the function subtracts 1 from the field value to ensure that it matches up values and responses correctly.)
To call this function, place the following statement in the routing section of your script:
NumSamp2CatQ(<question_name>, IOM.SampleRecord.Item["<fieldname>|"].Value)
In the example, you type:
NumSamp2CatQ(Gender, IOM.SampleRecord.Item["SampleGender"].Value)
NumSamp2CatQ(Mstat, IOM.SampleRecord.Item["SampleMstat"].Value)
If the respondent is a divorced woman, the incoming values would be SampleGender=2 and SampleMstat=3, and these would be matched with responses at position 1 (female) and position 2 (divorced) in Gender and Mstat respectively.
Converting categorical script data into numeric sample data
Now suppose that you want to do the same thing but the other way round; that is, to write categorical values into an integer field. The Region question is defined as:
Region "In which part of the country do you live?"
categorical [1..1]
{North, South, East, West};
SampleRegion is an integer (data type smallint) field in the sample record, so you need to find the position of the respondent’s chosen answer in the response list and copy that into the sample field. Type:
IOM.SampleRecord.Item["<name>"].Value = <question_name>.DefinedCategories().Find(<question_name>)
in the routing section of the script.
In this statement, DefinedCategories() is a function in the UNICOM Intelligence Function Library that scans the full response list as it is defined in the metadata section, and Find() is a function that searches for a value in a list. By concatenating the two functions after the name of the question whose value you want, you instruct the interviewing program to scan the full list of responses for a question and locate the answer that has been chosen from the list.
Since most sample fields start numbering from 1, it’s a good idea to add 1 to all categorical values when you convert them, so in this example you would write:
IOM.SampleRecord.Item["SampleRegion"].Value = Region.DefinedCategories().Find(Region) + 1
Converting text sample data into categorical script data
If a sample field contains a single word you might be able use that word as the answer to a categorical question. For example, if the respondent’s marital status appears in the sample record as Single and the question about marital status contains a response with that name, you can use a simple assignment statement in the routing section of the script to copy the sample data into the script. If the question is defined as:
Mstat "Please confirm your marital status" categorical [1..1]
{
  Single, Married, Divorced, Widowed,
  Partner "Living with partner"
};
you can set the answer to the question automatically with:
Empstat.Response.Value = IOM.SampleRecord.Item["SampleEmpstat"]
If the sample field contains more than one word and those words are separated by the same character or string of characters, you can use those values as the answers to a question with a multiple choice response list. However, you will need to write some code that preprocesses the string to remove the separators and then assigns each value to the question
Converting categorical script data into text sample data
You can write categorical data into sample records by using the Format function to pick up the category names of the responses you want to copy. Type, for example:
IOM.SampleRecord.Item["SampleEmpstat"] = Empstat.Response.Value.Format("a")
If the question allows more than one answer to be chosen, the answers are written into the sample field as a comma-separated list. All response names are written in lowercase regardless of how they appear in the questionnaire script.
See also
Working with sample records