Post by Farnood Saghi
So I have a variable in my datasheet named 'Symptoms', and in my questionnaire, the question associated to this variable gave the participants a list of symptoms and asked them to choose the ones they had experienced (multiple selections were allowed) and as far as scoring is concerned, the number of symptoms selected by each user would give me their score for that question. Anyway after importing my data to SPSS, this variable has been turned into a string consisting of all the symptoms selected by the user, and each of these selected symptoms is separated by a comma. I want to convert this to a numeric value, and I've figured the easiest way to do this is to write a custom function that counts the number of commas in each field and then adds 1 to it to give me the number of selected symptoms. (No of commas + 1). I know I'm supposed to use the syntax editor and manually write the syntax for this function but I don't know how to write it. Please help. Thanks in advance.
There might be a Python function to count the number of commas in a string. But here's an old fashioned NPR (no Python required) approach that works on a toy dataset I made up.
DATASET CLOSE ALL.
DATA LIST LIST / Symptoms (A150).
"fever, dry cough, fatigue"
"aches and pains, sore throat, diarrhoea, conjunctivitis"
"loss of taste or smell, skin rash, discolouration of fingers or toes, difficulty breathing or shortness of breath"
"chest pain or pressure, loss of speech or movement"
* Let scratch variable #L = length of string variable Symptoms.
COMPUTE #L = CHAR.LENGTH(Symptoms).
* Set N to 0 or 1 initially.
COMPUTE N = #L GT 0.
LOOP # = 1 to 500.
COMPUTE N = SUM(N, CHAR.SUBSTR(Symptoms,#,1) EQ ",").
END LOOP IF # GT #L.
FORMATS N (F2.0).
VARIABLE LABELS N "Number of symptoms".
OUTPUT from LIST:
The variables are listed in the following order:
LINE 1: Symptoms
LINE 2: N
Symptoms: fever, dry cough, fatigue
Symptoms: aches and pains, sore throat, diarrhoea, conjunctivitis
Symptoms: loss of taste or smell, skin rash, discolouration of fingers or toes, difficulty breathing or shortness of breath
Symptoms: chest pain or pressure, loss of speech or movement
Number of cases read: 6 Number of cases listed: 6