Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • working with string variable

    STATALIST,

    I need advice on how I could change the following variable:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str72 var1
    "M1=y (exclude through main part)"                                        
    "M1=n,M2=y (exclude through main part)"                                   
    "M1=n,M2=n,M3=y,M4=y (exclude through main part)"                         
    "M1=n,M2=n,M3=y,M4=n (goes to Branch),CH1=n (exclude through branch part)"
    end

    to

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(m1 m2 m3 m4 m5 m6 ch1 ch2)
    1 . . . . . . .
    0 1 . . . . . .
    0 0 1 1 . . . .
    0 0 1 0 . . 0 .
    end




    Regards,
    Last edited by Masoumeh Sanagou; 12 Nov 2024, 18:34.

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str72 var1
    "M1=y (exclude through main part)"                                        
    "M1=n,M2=y (exclude through main part)"                                  
    "M1=n,M2=n,M3=y,M4=y (exclude through main part)"                        
    "M1=n,M2=n,M3=y,M4=n (goes to Branch),CH1=n (exclude through branch part)"
    end
    
    local to_find M1 M2 M3 M4 M5 M6 CH1 CH2
    
    foreach t of local to_find {
        local tt = strlower("`t'")
        gen found = strlower(ustrregexs(1)) if ustrregexm(strupper(var1), "`t'\s*=\s*([YN])")
        gen byte `tt' = found == "y" if !missing(found)
        drop found
    }
    Note: This code can be simplified if we can rely on var1's rigidly following the model shown with regard to spacing, capitalization, commas, etc. Since that is something that is seldom reliable, this code offers a bit of additional flexibility in this regard: it is case insensitive and allows the = character to be offset with whitespace.

    Comment

    Working...
    X