Announcement

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

  • #16
    Mike:
    I almost missed your post, since I did not scroll back whilst reading Nick Cox's last reply. Thank you very much, your Code example is revealing!

    Comment


    • #17
      FWIW, the confusion between the -if- command and the -if- qualifier in Stata is a very common phenomenon, especially when people are starting to use it. I have often mused that it might be simpler if the -if- qualifier were called -where- or -wherever- instead of -if-, and -if- used only for the command. Just a thought.

      Comment


      • #18
        Reply to #15:

        Sorry; it is easy to lose track given your code and suggestions from other people on what they think you want to do.

        You're correct: your last program (post #5) creates an entire new variable which is missing except where you have a possibly estimated date.

        That's still not an approach that seems right for data management: every different application creates a new and separate date variable.

        Comment


        • #19
          I think Nick was interpreting your original if {....} else { ...} which does approximately change all the dates to a constant value. But as you noted from the start, it didn't do what you wanted.

          In Stata, the implementation of
          if {....} else { ...} that loops over observations is cond(., ., .). Thus,

          replace x = cond(missing(x),a,b)

          proceeds ("loops over") observation by observation, examining each one to check if x is missing; if true, { x = a }, else { x= b }. This can be elaborated and nested to create an equivalent to almost any if {....} else { ...} construct.

          hth,
          Jeph

          Comment


          • #20
            Originally posted by Nick Cox View Post
            Reply to #15

            That's still not an approach that seems right for data management: every different application creates a new and separate date variable.
            This is not a problem, since the original data reside in an external SQL database, which grows as new data from surveillance come in, and which does not contain those date variables that are only created by Stata. Everytime I do an Analysis, I reload the updated file from the server and the Stata version is recalculated.

            Comment


            • #21

              Originally posted by Jeph Herrin View Post
              In Stata, the implementation of if {....} else { ...} that loops over observations is cond(., ., .).
              Thank you, Jeph - It is good to know that this useful command exists in Stata; I know this kind of command from other environments "iif(.;.;.)"

              I have to say that I am amazed by the many replies I have received to my initial post. Not only several good ways for programming the specific example problem - I think I have finally grasped the difference between Stata and the programs I have been working with. Thank you all!

              Comment


              • #22
                Glad you feel you made real progress. cond() was specifically mentioned in posts #2 and #3 here.

                Comment


                • #23
                  I find cond() awkward to use -- if there are nested conditions I think the code gets hard to read and more prone to mistakes. I therefore tend to write out code that is less efficient but easier to read and follow. If/then/else constructions are pretty easy to code and understand. I wish there was something more like if/then/else that was an alternative to cond().
                  -------------------------------------------
                  Richard Williams, Notre Dame Dept of Sociology
                  Stata Version: 17.0 MP (2 processor)

                  EMAIL: [email protected]
                  WWW: https://www3.nd.edu/~rwilliam

                  Comment


                  • #24
                    I'm sure that's a common feeling. It was discussed, pro and con, for cond() in

                    http://www.stata-journal.com/sjpdf.h...iclenum=pr0016

                    Syntax design is a dark and difficult art. Concise syntax is widely praised but languages that prized brevity above else have remained niche products, although their experienced users become passionate about them. In the language J

                    Code:
                     
                    ave  =: +/ % #
                    is an entire program, but it takes a chapter to explain it at all carefully. But conciseness can at least be assessed in terms of lines or length of code. Other dimensions are more difficult to name, let alone assess. Someone else can describe a syntax as intuitive when you think privately that all that means is they are familiar with it, and the same applies in reverse.

                    To the point here:

                    1. Looping over observations can look more readable, but as underlined several times in this thread, it is slow.

                    2. A replace using cond() can usually be rewritten as two or more separate replace commands with different if qualifiers, and most of us presumably do that often.

                    3. There is always scope for writing or using a quite different command, such as recode.

                    Comment


                    • #25
                      Originally posted by Jeph Herrin View Post
                      It looks as if you are trying to write a program (-program makdat-) which takes as arguments a date variable, a day, a month, and year, and then creates a new date variable according to some logic...... If this is correct, then the program you want is probably


                      Code:
                      program makedat
                      version 13
                      args datevar dd mm yyyy
                      tempvar day mon year
                      gen `day'= cond(inrange(`dd',1,31),`dd',15)
                      gen `mon' = cond(inrange(`mm',1,12),`mm',7)
                      gen `year' = cond(inrange(`yyyy',1,2999),`yyyy',2012)
                      
                      gen `datevar' = mdy(`mon',`day',year')
                      end
                      This creates three new variables for month, day, year where the missing values are filled in, and then creates the new datevar using those. Since they are temporary variables, they disappear when the program ends.
                      Thank you again, Jeph, for this improved version of the little program. However, when I call the program with four arguments: makedat GebDat GebD GebM GebJ,
                      corresponding to the items following "args", the following error message is issued:
                      "varlist required
                      (error occurred while loading makedat.ado)
                      r(100);"
                      I would have thought that the four arguments constituted the necessary varlist, so why does the error occur? I could not find an answer in the documentation.

                      Comment


                      • #26
                        Code:
                        gen `datevar' = mdy(`mon', `day', year')
                        should be

                        Code:
                        gen `datevar' = mdy(`mon', `day', `year')
                        The single quotation marks should all be paired.

                        Comment


                        • #27
                          Originally posted by Jeph Herrin View Post
                          It looks as if you are trying to write a program (-program makdat-) which takes as arguments a date variable, a day, a month, and year, and then creates a new date variable according to some logic...... If this is correct, then the program you want is probably


                          Code:
                          program makedat
                          version 13
                          args datevar dd mm yyyy
                          tempvar day mon year
                          gen `day'= cond(inrange(`dd',1,31),`dd',15)
                          gen `mon' = cond(inrange(`mm',1,12),`mm',7)
                          gen `year' = cond(inrange(`yyyy',1,2999),`yyyy',2012)
                          
                          gen `datevar' = mdy(`mon',`day',year')
                          end
                          This creates three new variables for month, day, year where the missing values are filled in, and then creates the new datevar using those. Since they are temporary variables, they disappear when the program ends.
                          Thank you again, Jeph, for this improved version of the little program. However, when I call the program with four arguments: makedat GebDat GebD GebM GebJ,
                          corresponding to the items following "args", the following error message is issued:
                          "varlist required
                          (error occurred while loading makedat.ado)
                          r(100);"
                          I would have thought that the four arguments constituted the necessary varlist, so why does the error occur? I could not find an answer in the documentation.

                          Comment


                          • #28
                            Sorry, my previous post #27 (identical to #25) was due to a bad manipulation. The Statalist prompted me whether a previously saved text should be recovered or discarded, so I thought #25 was not sent for some reason.

                            Thank you, Nick, for your reply. However, I checked the ado-file I am using and the left quote character ` is actually not missing in it, only in the Code shown in Jeph's post #8 I quoted. So this cannot be the reason for the error message.

                            Comment


                            • #29
                              Nick, I found the reason for the error. Very stupid, indeed. There were a few other versions of program makedat in the same ado file (commented out between /* */) and when I inserted Jephs' Code on top, it escaped me the that the final codeword "end" was duplicated. Sorry for that.

                              Comment


                              • #30
                                i have this program wrote in SPSS how can i convert it to stata program:

                                do if (preg = 0 and amenor = 0).
                                + compute fecund = 1.
                                + if (UN13U = 4 or (UN13U = 3 and UN13N > 6) or (UN13U = 9 and UN13N>= 94 and UN13N<=96)) fecund = 0.
                                + if (UN11B = "B" or UN11C = "C" or UN11D = "D" or UN11E="E") fecund = 0.
                                + if (UN6 = 3 or (UN7U = 9 and UN7N = 94)) fecund = 0.
                                + if (usemeth = 0 and nob5yrs = 1 and m5yrs = 1) fecund = 0.
                                end if.

                                Comment

                                Working...
                                X