Announcement

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

  • Stata locals & loops using if condition

    Hi,

    I'm having a problem writing the correct loop code using an if condition.
    I'm using Stata 14.1 for Windows.

    I have set locals for each drug name & a local combining all the drug names. I have then generated a new variable for each of the drugs as follows:

    local drug1 `" possible string names for drug "'
    local drug2 `" possible string names for drug "'
    local drug3 `" possible string names for drug "'
    local alldrugs "drug1 drug2 drug3"

    foreach var of local alldrugs {
    gen `var'=.
    }

    I now want to replace the value of each new variable (drug1-drug3) with 1 if another variable (comdescription) contains the specific strings contained in the locals drug1-drug3 (i.e. replace the value of drug1 with 1 if comdescription matches any string held in the local drug1 and replace drug2=1 if string in comdescription matches any in drug2 local, etc.).

    So far I have written the following code, but it is not correct & I am unsure about how to write the if condition. I want the if statement to say "if the name of the variable equals the name of the local".

    foreach local of local alldrugs {
    foreach varname of local `local' {
    foreach x of local `local' {
    if `varname'[1]==`local'[1] { // if condition not correct (if the name of the variable equals the name of the local
    replace `varname'=1 if strmatch(lower(comdescription),"*`x'*")
    }
    }
    }
    }

    After a number of real changes have been made, the error I get is that one of my strings held in drug1 is not found.

    I could do this individually without the loops but I would like to learn how to use loops effectively & would really appreciate any advice on this matter.

    Thank you for your time.

    Best wishes,
    Bryony

  • #2
    Welcome to Statalist, Bryony.

    Here is some sample code that may start you in a useful direction.
    Code:
    local drug1 `" ibuprofen nuprin "'
    local drug2 `" advil acetominaphen "'
    local drug3 `" aspirin bayer "'
    local alldrugs "drug1 drug2 drug3"
    
    clear
    input str20 comd
    nuprin
    bayer
    advil
    ibuprofen
    ginseng
    end
    
    foreach drug of local alldrugs {
        gen `drug' = 0
    }
    
    foreach drug of local alldrugs {
        foreach name of local `drug' { 
            replace `drug' = 1 if comd=="`name'"
        }
    }
    Code:
    . list, clean
    
                comd   drug1   drug2   drug3  
      1.      nuprin       1       0       0  
      2.       bayer       0       0       1  
      3.       advil       0       1       0  
      4.   ibuprofen       1       0       0  
      5.     ginseng       0       0       0

    Comment


    • #3
      Hi, I have a similar problem with my code. I don´t understand what I´m doing wrong. I had an invalid syntax but I´m not sure what it is. I´m using STATA 16.

      Somebody can help me please?

      egen p402mean = rowmean(p4022012 p4022013 p4022014 p4022015 p4022016 p4022017)

      forvalues 2012(1)2017 {
      cap drop p402mean`i' p402_maxyear_`i'
      egen p402mean`i'= mean(p402mean`i')
      g p402_maxyear_`i'=1 if p402`i'>p402mean`i'
      replace p402_maxyear_`i'=0 if p402`i'<p402mean`i'
      replace p402_maxyear_`i'=. if p402`i'==.
      }


      Comment


      • #4
        The error is right at the top of your loop: -forvalues 2012(1)2017{- is the syntax error. Based on your frequent use of `i' inside the loop, I'm guessing what you meant is -forvalues i = 2012(1)2017 {-.

        However, when you fix that you encounter another error: -egen p402mean`i' = mean(p402mean`i')- cannot work because p402mean`i' has been -drop-ped in the immediately preceding command. (And if it weren't dropped, you'd still have an error because then you would be attempting to -egen- a variable that already exists. Since I don't know what you're actually trying to do with this code, I can't suggest a correction.

        If you can describe what the code is intended to calculate, I might be able to correct your code. Or, actually, it is most likely that no loop is even required and that the correct approach is to first -reshape- the data to long layout and then do something extremely simple after that. But, I'm just speculating now.

        Comment

        Working...
        X