Just for fun, here is a one-liner to do this using the SPSSINC TRANS extension command.
spssinc trans result = systolic diastolic
/formula "(bp[:3], bp[3:]) if str.startswith(bp, '1') else (bp[:2], bp[2:])".
The resulting values are numeric. If SPSSINC TRANS is not already installed or is not the latest version, it can be installed or updated via Extensions > Extension Hub.
--
Original Message:
Sent: 1/5/2024 4:45:00 PM
From: David Dwyer
Subject: RE: Regarding string variable
Thanks @Art Jack!
As usual, I'm over-thinking. That's pretty elegant.
------------------------------
David Dwyer
SPSS Technical Support
IBM Software
------------------------------
Original Message:
Sent: Fri January 05, 2024 02:57 PM
From: Art Jack
Subject: Regarding string variable
You could also do something like this.
dataset close all.
DATA LIST FREE/ id (F1) bp (A6).
BEGIN DATA
1 "9075"
2 "12080"
3 "99101"
4 "180110"
END DATA.
LIST.
NUMERIC systolic diastolic (F3).
do if char.index(char.substr(lower(bp),1,1),'1') | char.index(char.substr(lower(bp),1,1),'2').
COMPUTE systolic = NUMBER(CHAR.SUBSTR(bp,1,3),F3).
COMPUTE diastolic = NUMBER(CHAR.SUBSTR(bp,4),F3).
else.
COMPUTE systolic = NUMBER(CHAR.SUBSTR(bp,1,2),F3).
COMPUTE diastolic = NUMBER(CHAR.SUBSTR(bp,3),F3).
end if.
------------------------------
Art Jack
Original Message:
Sent: Thu January 04, 2024 11:57 AM
From: David Dwyer
Subject: Regarding string variable
Hi @Khalid Orayj,
You've really hamstrung yourself with the data entry. Here is one approach to resolving your issue. As I saw it, 4 digit values were easy, 6 digit values were easy, but 5 digit values presented a problem. So I arbitrarily decided to parse the value differently if the leftmost value of the 'bp' variable was 1 (100<= systolic <200) versus any other value. If it actually is possible to have a valid systolic value of 200 (or more), then my approach will still have problems for you.
------------------------------
David Dwyer
SPSS Technical Support
IBM Software
Original Message:
Sent: Tue December 26, 2023 01:47 PM
From: Khalid Orayj
Subject: Regarding string variable
Hi,
I have a string variable (blood pressure) that contains numbers (it can be Easly converted to numeric with no issue). the value under this variable is containing both systolic and diastolic blood pressure readings together like : "12080: which means the blood pressure is 120/80 and if its "180110: this means the blood pressure is 180/110. I want to create two new variables called "systoilc_blood_pressure" and "diastolic_blood_pressure". The systolic one should extract the first two digits of (blood pressure) if the (blood pressure) contains 4 digits and should extract the first three digits if the (blood pressure) contains 5 or 6 digits. The diastolic one should extract the last two digits of the (blood pressure) if it contains 4 or 5 digits, and should extract the last three digits if the (blood pressure) contains 6 digits. I need a syntax to do that. I have been struggling on this problem for one month until now. Any help will be appreciated.
Khalid
------------------------------
Khalid Orayj
------------------------------