Announcement

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

  • Extracting substrings from variables

    I tried extracting substrings from variable names to be used as part of a condition to populate another variable (modality). But it seems the code is not working, that is, nothing is being assigned to the new variable (modality). I will appreciate your advice in this regard.

    These are the variable names: fac_idx fac_link fac_oth fac_pitc fac_pmtct fac_sns fac_sti fac_tb fac_vct modality oss_idx oss_link oss_mob oss_oth oss_pitc oss_pmtct oss_sns oss_vct. Each of them are indicator variables of 0 and 1.

    The code is as written below.

    Thanks.

    Code:
    capture drop modality
    gen modality = "."
    quietly ds fac_* oss_* 
    global vars `r(varlist)'
    global omit fac_cost
    global want : list vars - omit
    foreach want of global want {
            replace modality = "vct" if substr("`want'", 5, .) == "vct" & `want'== 1
            replace modality = "tb" if substr("`want'", 5, .) == "tb" & `want'== 1
            replace modality = "sti" if substr("`want'", 5, .) == "sti" & `want'== 1
            replace modality = "sns" if substr("`want'", 5, .) == "sns" & `want'== 1
            replace modality = "pmtct" if substr("`want'", 5, .) == "pmtct" & `want'== 1
            replace modality = "pitc" if substr("`want'", 5, .) == "pitc" & `want'== 1
            replace modality = "oth" if substr("`want'", 5, .) == "oth" & `want'== 1
            replace modality = "link" if substr("`want'", 5, .) == "link" & `want'== 1
            replace modality = "idx" if substr("`want'", 5, .) == "idx" & `want'== 1
            replace modality = "mob" if substr("`want'", 5, .) == "mob" & `want'== 1
    }

  • #2
    Your problem is that in
    Code:
    global want : list vars - omit
    the macro : list command assumes that all the macro names to the right of the colon are local macro names. It is possible to indicate that they are global macro names, but using global macros is generally not a good idea when a local macro will do.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(fac_idx fac_link fac_oth oss_idx oss_link oss_oth)
    1 0 0 0 0 0
    0 1 0 0 0 0
    0 0 1 0 0 0
    0 0 0 1 0 0
    0 0 0 0 1 0
    0 0 0 0 0 1
    end
    
    gen modality = "."
    quietly ds fac_* oss_* 
    local vars `r(varlist)'
    local omit fac_cost
    local wanted : list vars - omit
    
    foreach want of local wanted {
            replace modality = "vct" if substr("`want'", 5, .) == "vct" & `want'== 1
            replace modality = "tb" if substr("`want'", 5, .) == "tb" & `want'== 1
            replace modality = "sti" if substr("`want'", 5, .) == "sti" & `want'== 1
            replace modality = "sns" if substr("`want'", 5, .) == "sns" & `want'== 1
            replace modality = "pmtct" if substr("`want'", 5, .) == "pmtct" & `want'== 1
            replace modality = "pitc" if substr("`want'", 5, .) == "pitc" & `want'== 1
            replace modality = "oth" if substr("`want'", 5, .) == "oth" & `want'== 1
            replace modality = "link" if substr("`want'", 5, .) == "link" & `want'== 1
            replace modality = "idx" if substr("`want'", 5, .) == "idx" & `want'== 1
            replace modality = "mob" if substr("`want'", 5, .) == "mob" & `want'== 1
    }
    list, clean noobs
    Code:
    . list, clean noobs
    
        fac_idx   fac_link   fac_oth   oss_idx   oss_link   oss_oth   modality  
              1          0         0         0          0         0        idx  
              0          1         0         0          0         0       link  
              0          0         1         0          0         0        oth  
              0          0         0         1          0         0        idx  
              0          0         0         0          1         0       link  
              0          0         0         0          0         1        oth  
    
    .

    Comment


    • #3
      Actually, the lengthy series of -replace- statements is not necessary. You can get the same result with:
      Code:
      foreach want of varlist `wanted' {
          replace modality = substr(`"`want'"', 5, .) if `want' == 1
      }

      Comment


      • #4
        thank you very much, your responses were very useful
        Last edited by Kehinde Atoloye; 31 Jan 2023, 02:40.

        Comment


        • #5
          .
          Last edited by Kehinde Atoloye; 31 Jan 2023, 02:40.

          Comment

          Working...
          X