Announcement

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

  • Multiple line -if- conditional on variable

    I have (quite stupidly) have code of this type

    if market=="a" {
    replace o=1
    replace e=1
    replace state="w"
    replace date="10/8/16"
    }

    I have this for about 600 markets and several statements inside so my do file stretches to over 9000 lines. I have now realized that the -if- does not work properly when conditioned on a specific value of a variable. Is there any quick way to implement a change for all of 600 market -if- conditions? Would be very grateful for some help!

  • #2
    Welcome to Statalist.

    There is no easy way to correct the code you have written. What we can do is write a Stata program to read a file containing the code you've written and create a new file with corrected code. Perhaps the following example will start you in a fruitful direction.
    Code:
    type oldcode.do
    clear
    import delimited using oldcode.do, delimiters("~") // use a delimiter not in the code
    rename v1 code
    list, clean noobs
    
    generate str40 ifcond = ""
    replace ifcond = substr(code,1,strpos(code,"{")-1)
    replace ifcond = ifcond[_n-1] if ifcond==""
    drop if strpos(code,"{") | strpos(code,"}")
    generate str50 newcode = trim(code) + " " + trim(ifcond)
    list, clean noobs
    
    drop code ifcond
    outfile using newcode.do, noquote replace
    type newcode.do
    Code:
    . type oldcode.do
    if market=="a" {
    replace o=1
    replace e=1
    replace state="w"
    replace date="10/8/16"
    }
    
    . clear
    
    . import delimited using oldcode.do, delimiters("~") // use a delimiter not in the code
    (1 var, 6 obs)
    
    . rename v1 code
    
    . list, clean noobs
    
                          code  
              if market=="a" {  
                   replace o=1  
                   replace e=1  
             replace state="w"  
        replace date="10/8/16"  
                             }  
    
    . 
    . generate str40 ifcond = ""
    (6 missing values generated)
    
    . replace ifcond = substr(code,1,strpos(code,"{")-1)
    (1 real change made)
    
    . replace ifcond = ifcond[_n-1] if ifcond==""
    (5 real changes made)
    
    . drop if strpos(code,"{") | strpos(code,"}")
    (2 observations deleted)
    
    . generate str50 newcode = trim(code) + " " + trim(ifcond)
    
    . list, clean noobs
    
                          code            ifcond                                 newcode  
                   replace o=1   if market=="a"               replace o=1 if market=="a"  
                   replace e=1   if market=="a"               replace e=1 if market=="a"  
             replace state="w"   if market=="a"         replace state="w" if market=="a"  
        replace date="10/8/16"   if market=="a"    replace date="10/8/16" if market=="a"  
    
    . 
    . drop code ifcond
    
    . outfile using newcode.do, noquote replace
    
    . type newcode.do
    replace o=1 if market=="a"
    replace e=1 if market=="a"
    replace state="w" if market=="a"
    replace date="10/8/16" if market=="a"

    Comment


    • #3
      Let me add a second approach. If you have a table from which you copied these substitutions, you can use merge ..., update replace to apply the corrections to the master data. (This is the most efficient way to approach the problem, takes less time than writing 9000 lines of code.) The code would look something like this.
      Code:
      import excel using updates.xlsx, firstrow clear // everybody uses Excel to make tables of stuff, it doesn't have to be, though
      list, clean
      save updates, replace
      
      use master, clear
      list, clean
      merge m:1 market using updates, update replace
      list, clean
      Code:
      . import excel using updates.xlsx, firstrow clear
      
      . list, clean
      
             market   o   e   state      date  
        1.        a   1   1       w   10/8/16  
      
      . save updates, replace
      file updates.dta saved
      
      .
      . use master, clear
      
      . list, clean
      
              id   market   o   e   state     date  
        1.   101        a   0   0       q   5/8/16  
        2.   102        a   0   0       q   5/8/16  
        3.   103        b   0   0       q   5/8/16  
      
      . merge m:1 market using updates, update replace
      
          Result                           # of obs.
          -----------------------------------------
          not matched                             1
              from master                         1  (_merge==1)
              from using                          0  (_merge==2)
      
          matched                                 2
              not updated                         0  (_merge==3)
              missing updated                     0  (_merge==4)
              nonmissing conflict                 2  (_merge==5)
          -----------------------------------------
      
      . list, clean
      
              id   market   o   e   state      date                    _merge  
        1.   101        a   1   1       w   10/8/16   nonmissing conflict (5)  
        2.   102        a   1   1       w   10/8/16   nonmissing conflict (5)  
        3.   103        b   0   0       q    5/8/16           master only (1)
      Last edited by William Lisowski; 12 Mar 2019, 12:58.

      Comment

      Working...
      X