Announcement

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

  • for each and replace function

    Hi all

    I am trying to use a macro to generate a 'drugs' yes(1) or no(0) variable from 3 variables coded 1yes, 0no - glue, cannabis and othersub.

    Essentially if an individual has said yes to either glue, cannabis or othersub I would like drugs to be 1 - regardless if any other is missing.
    If glue, cannabis or othersub no for all I would like this to be no - 0.
    If glue, cannabis or othersub have a mixture of no and missing I would like to have this as a missing variable '.'

    So far I have this code, but its not working - it doesn't come up with an error but all values are 0.

    local druglist "glue cannabis othersub"
    display "druglist"

    g drugs= 0
    foreach var of local druglist {
    replace drugs = 1 if `var' ==1
    }

    foreach var of local druglist {
    replace "drug = . if 'var' ==.
    }

    Any help would be super appreciated.


  • #2
    Hi em lowthian (If that's you real name),

    so I spot several unecessary quotation-marks in your code, a ' instead of ` and a spelling error. Try this:
    Code:
    local druglist glue cannabis othersub
    g drugs= 0
    foreach var of local druglist{
    replace drugs = 1 if `var' ==1
    }
    
    foreach var of local druglist {
    replace drugs = . if `var' ==.
    }
    Hoped that helped,

    Tim Umbach
    Last edited by Tim Umbach; 22 Jun 2017, 04:57.

    Comment


    • #3
      The last loop breaks your own rules as any missing will overwrite any occurrence of 1.

      You don't need any loops here at all. Hence, you don't need local macros driving the loop.

      Your rules boil down to

      Code:
      gen drugs = cond(inlist(1, glue, cannabis, othersub), 1, cond(missing(glue, cannabis, othersub), ., 0)) 
      That may look trickier than it really is: it's a translation of

      1. if any response is 1, then return 1

      if inlist(1, glue, cannabis, othersub) then return 1

      2. otherwise if any response is missing, then return missing

      if missing(glue, cannabis, othersub) then return .

      3. otherwise return 0.


      Note that foreach and replace are commands, not functions.
      Last edited by Nick Cox; 22 Jun 2017, 05:09.

      Comment


      • #4
        HI both,

        thanks for your detailed replies - I really appreciate them both and have learnt something from both of them.

        Nick - thank you for this. However, will it incorporate where people say no in glue, othersub and cannabis and give a value of 0?

        Em

        Comment


        • #5
          Do try it. #3 controls the outcome.

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input glue cannabis othersub
          0 0 0
          end
          gen drugs = cond(inlist(1, glue, cannabis, othersub), 1, cond(missing(glue, cannabis, othersub), ., 0))
          
          list
          
               +------------------------------------+
               | glue   cannabis   othersub   drugs |
               |------------------------------------|
            1. |    0          0          0       0 |
               +------------------------------------+

          Comment


          • #6
            I will absoutely, I'm just stuck recoding other bits at the moment - theres nothing to lose in trying it. I will get back to you - thank you :-)

            When you say that foreach and replace are functions - I wasn't sure what you were getting at? Could you explain further please?

            Comment


            • #7
              No; I said this, which is the opposite.

              Note that foreach and replace are commands, not functions.
              In Stata functions and commands are disjoint. The words are not synonyms.

              You're free to regard the distinction as pedantic, but it is correct, and (for example)

              1. If experienced users see Stata terminology being used incorrectly, they have to work harder to translate to what the poster really means and wants. (At one extreme we occasionally get. posters failing completely to talk about their data in Stata terms and their reward is often that their questions just don't get answered.)

              2. New users benefit from knowing correct terminology. Their meaning is clearer and they understand say that functions are documented in the manual on functions and obey quite different syntax from commands.

              3. What is true of other software is true of other software, but that's for other software and their forums
              Last edited by Nick Cox; 22 Jun 2017, 07:32.

              Comment


              • #8
                Sorry - my mistake. That is interesting thank you. It worked though, so thank you! I will probably need that function again so I will keep it on tab!

                Many super thanks!

                Comment


                • #9
                  There is a tutorial at http://www.stata-journal.com/sjpdf.h...iclenum=pr0016

                  Comment


                  • #10
                    Thank you so much!

                    Comment


                    • #11
                      Dear all,

                      I would be thankful if you could help me with the following error. I want to use foreach command to replace the values of multiple variables but I receive a following error:
                      foreach var of varlist termination_date - p_region {
                      replace 'var' = "na" if _merge == 2
                      }

                      ' invalid name

                      Comment


                      • #12
                        Wrong opening quotation mark.

                        Code:
                        replace `var' = "na" if _merge == 2

                        Comment

                        Working...
                        X