Announcement

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

  • Loops and macros in Stata

    Dear Stata List Members,
    Would greatly appreciate it if any of you could provide some suggestions about the problem below:

    I've about 41 variables (barriers to implementation of projects) which were collected at the pre and the post intervention phases. The variable naming convention is as follows: 'a1', 'b1', 'c1', ..., 'z1' which denote the pre values and 'a2', 'b2', 'c2', ..., 'z2' denote the post values, respectively.
    I need to create new vars, 'predict' [a,b,c,...z], which would have a value of 1
    if both the pre values and the post values for that barrier are equal to -1 OR one of them is -1 and the other is +3;
    in other words,
    if both 'a1' (i.e. pre values) and 'a2' (i.e. post values) are equal to -1 OR one of these vars is -1 (e.g., a1) and the other (e.g., a2) is +3. The process has to be repeated for all barriers.

    Then for each team i.e. subject, I have to add all 'predict' vars (i.e. for each subject/team, create a sum of predict'a', predict'b', predict'c',..., predict'z').

    Is there an efficient way to do this? I was reading about loops and macros which are new topics to me; I tried to use them, with my little knowledge gained through online searches but was unable to do so; not sure how to approach the problem. Would greatly appreciate any suggestion any of you might provide.

    Below is what I did to complete the 1st step which is to generate 'predict' vars do but I got an error message that too many variables were specified. I'm also not sure how the 2nd step can be done- adding all 'predict' vars for each subject/team.

    local cfir_intervention InterventionSource EvidenceStrengthQuality Relativeadvantage Adaptability Trialability Complexity DesignQualityandPackaging Cost
    forvalues i=1/2 {
    generate predict`cfir_intervention'=.
    replace predict`cfir_intervention'=1 if cfir_intervention`i'==-1 & cfir_intervention`i'+1==-1
    replace predict`cfir_intervention'=1 if cfir_intervention`i'==-1 & cfir_intervention`i'+1==3
    replace predict`cfir_intervention'=1 if cfir_intervention`i'==3 & cfir_intervention`i'+1==-1
    }

    Thanks very much for your time.
    Sincerely
    Musarrat

  • #2
    It is difficult to understand your data without a concrete example (see section 12 in the FAQ) but I guess you are trying to do a loop over variables. The command
    Code:
    generate predict`cfir_intervention'=.
    is evaluated by Stata as
    Code:
    generate predictInterventionSource EvidenceStrengthQuality Relativeadvantage Adaptability Trialability Complexity DesignQualityandPackaging Cost=.
    which results in
    Code:
    . generate predictInterventionSource EvidenceStrengthQuality Relativeadvantage Adaptability Trialability Complexity DesignQualityandPackaging Cost=.
    too many variables specified
    r(103);
    because you can only generate one variable at a time. Have a look at
    Code:
    help foreach
    and read about
    Code:
    foreach lname of varlist list

    Comment


    • #3
      Thanks very much. I'm not sure what you showed matches with what I'm trying to do or simplifies the process. But thanks again for taking the time to find a solution.
      I played around a bit more with loops and macros and this time I could get what I wanted for facilitators (as shown below) but the process didn't work for the barriers.Would greatly appreciate it if anyone could give me some clues as to where the problem is.

      local cfir_intervention InterventionSource EvidenceStrengthQuality Relativeadvantage Adaptability Trialability Complexity DesignQualityandPackaging Cost

      foreach m of local cfir_intervention {
      generate enabler`m'=.
      local i=1
      replace enabler`m'=1 if `m'`i'>=1 & `m'`i'+1>=1
      }

      egen newtotal= anycount(enablerInterventionSource-enablerCost ), values(1)


      Note: The condition shown above was for facilitators and the results for the above were okay; when I used the below ones for barriers, no results were shown i.e. no vars were created but no error message was shown!

      local cfir_intervention InterventionSource EvidenceStrengthQuality Relativeadvantage Adaptability Trialability Complexity DesignQualityandPackaging Cost
      foreach m of local cfir_intervention {
      generate barrier`m'=.
      local i=1
      replace barrier`m'=1 if `m'`i'==-1 & `m'`i'+1==-1
      replace barrier`m'=1 if `m'`i'==-1 & `m'`i'+1==3
      replace barrier`m'=1 if `m'`i'==3 & `m'`i'+1==-1
      }


      Thanks very much.

      Comment


      • #4
        Please have a look at the FAQ. Stata commands and output are easier to read if you use CODE delimiters. The FAQ also recommends the following:

        Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
        You wrote that "no vars were created" but I can demonstrate that your commands create 8 variables.
        Code:
        local cfir_intervention InterventionSource EvidenceStrengthQuality Relativeadvantage ///
          Adaptability Trialability Complexity DesignQualityandPackaging Cost
        foreach m of local cfir_intervention {
          generate barrier`m'=.
        }
        describe
        
        Contains data
          obs:             0                          
         vars:             8                          
         size:             0                          
        ------------------------------------------------------------------------------------------------
                      storage   display    value
        variable name   type    format     label      variable label
        ------------------------------------------------------------------------------------------------
        barrierInterv~e float   %9.0g                 
        barrierEviden~y float   %9.0g                 
        barrierRelati~e float   %9.0g                 
        barrierAdapta~y float   %9.0g                 
        barrierTriala~y float   %9.0g                 
        barrierComple~y float   %9.0g                 
        barrierDesign~g float   %9.0g                 
        barrierCost     float   %9.0g                 
        ------------------------------------------------------------------------------------------------
        Sorted by: 
             Note: Dataset has changed since last saved.
        If you meant something else, for example that all newly created variables were empty, then you have to provide more information. A good way to start would be to share an excerpt from your data. You can use the dataex package for this, which is described in the FAQ.

        Comment


        • #5
          Did you see the technique Friedrich used in post #2 to figure out how your commands with macros are being interpreted by Stata? You should apply that technique to the commands you show us in post #3 to figure out what you are doing incorrectly.

          The first time through the loop, your command
          Code:
          replace barrier`m'=1 if `m'`i'==-1 & `m'`i'+1==-1
          had cfir_intervention substituted for `m' and 1 substituted for `i' so Stata ran the command
          Code:
          replace barriercfir_intervention=1 if cfir_intervention1==-1 & cfir_intervention1+1==-1
          which is not what you want. The command that Stata runs should be
          Code:
          replace barriercfir_intervention=1 if cfir_intervention1==-1 & cfir_intervention2==-1
          and working backwards, that suggests your command should be
          Code:
          replace barrier`m'=1 if `m'1==-1 & `m'2==-1
          noting that there is no need to use a macro for the pre and post values of 1 and 2. Similar changes are needed in the other two commands in that loop.

          I will add that the code you showed for the enabler variables is also not correct, as the command you are running in the loop is
          Code:
          replace enablercfir_intervention=1 if cfir_intervention1>=1 &cfir_intervention1+1>=1
          Let me add the following advice. In post #1 you wrote

          I was reading about loops and macros which are new topics to me; I tried to use them, with my little knowledge gained through online searches but was unable to do so; not sure how to approach the problem.
          Learning Stata's capabilities is a problem you should approach systematically, and to do so, you should utilize Stata resources, not random searches of online material.

          When I began using Stata in a serious way, I started as have others here, by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. All of these manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through Stata's Help menu. The objective in doing this was not so much to master Stata as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax, and know how to find out more about them in the help files and manual.

          Stata supplies exceptionally good documentation that amply repays the time spent studying it. The path I followed surfaces the things you need to know to get started in a hurry and to work effectively.
          Last edited by William Lisowski; 20 May 2017, 11:03.

          Comment


          • #6
            Thanks to you both, Mr. Huebler and Mr. Lisowski, for taking the time to to read my questions and give advice.
            Sincerely
            MN

            Comment

            Working...
            X