SPSS Statistics

Expand all | Collapse all

Substring variable

  • 1.  Substring variable

    Posted 30 days ago
    Good evening, 
    I was wondering if someone could help me out here. 
    I'm working with the national inpatient sample database which uses ICD 10 codes for diseases.

    I have the following variables: I10_DX1 through I10_DX40

    Each of these variables have ICD codes for example stroke: Every ICD code for stroke starts with "I63"

    I would like to create a new variable called "STROKE" using syntax to loop through 10_DX1 through I10_DX40 looking for "I63" and give a value of 1 if present and 0 if not present. I have been able to do this on STATA however STATA is too slow. I pasted the code I used in STATA as an example. 

    generate STROKE=0
    foreach var of varlist I10_DX2-I10_DX40 {
    replace STROKE=1 if substr(`var',1,4)=="I63"



    Christian Torres

  • 2.  RE: Substring variable

    Posted 30 days ago
    You could try:

    compute stroke=0.
    do repeat I=I10_DX2 to I10_DX40.
    if char.substr(I,1,4) eq "I63" stroke=1.
    end repeat.

  • 3.  RE: Substring variable

    Posted 30 days ago
    I think you meant char.substr(I, 1, 3), since the third parameter is the length.


  • 4.  RE: Substring variable

    Posted 30 days ago
    True. I noticed that I forgot to change that in the original code only after I posted. Thanks.

    Rick Marcantonio
    Quality Assurance