Erin
Erin

Reputation: 11

Finding a number in a string and creating a new true/false variable

I have a two string variables (DiagnosisPrimary & DiagnosisOther) that have diagnosis coded into numerical values and delineated with '~' . So person one was diagnosed with '20' on their first visit and then nothing on their second visit.

DiagnosisPrimary
20~
12~~~
20~20~
12~12~
12~~
~~~~~
12~12

If 20 represents having been diagnosed with depression, I want to see if there is a '20' among the string. Basically, find out who was diagnosed with depression at any of their visits. In the past I have used the formula below, but now the data is a mix of numerical and text it is no longer working.

COMPUTE Dx_Depression =ANY(20, DiagnosisPrimary) OR ANY(20, DiagnosisOther).

I want to create a new variable with a true/false response.

Any help much appreciated!

Upvotes: 0

Views: 40

Answers (1)

horace_vr
horace_vr

Reputation: 3166

You should use the CHAR.INDEX function, which returns the position of a string in another string (or 0 if the sub string does not exist)

COMPUTE Dx_Depression = (CHAR.INDEX(DiagnosisPrimary, '~20~') >0) or 

(CHAR.INDEX(DiagnosisPrimary, '~20')=char.len(DiagnosisPrimary)-3) or 

(CHAR.INDEX(DiagnosisPrimary, '20~')=1) or 

(CHAR.INDEX(DiagnosisOthers, '~20~') >0) or 

(CHAR.INDEX(DiagnosisOthers, '~20')=char.len(DiagnosisOthers)-3) or 

(CHAR.INDEX(DiagnosisOthers, '20~')=1)

Upvotes: 1

Related Questions